[vlc-commits] dump: clean up and simplify

Rémi Denis-Courmont git at videolan.org
Thu Nov 29 16:45:42 CET 2012


vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Tue Nov 27 23:56:34 2012 +0200| [4d6f7ee78c209ac82f76d2c322f88bb8a0eabcd0] | committer: Rémi Denis-Courmont

dump: clean up and simplify

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=4d6f7ee78c209ac82f76d2c322f88bb8a0eabcd0
---

 modules/demux/demuxdump.c |  147 ++++++++++++++-------------------------------
 1 file changed, 44 insertions(+), 103 deletions(-)

diff --git a/modules/demux/demuxdump.c b/modules/demux/demuxdump.c
index 33fc9a1..960fe36 100644
--- a/modules/demux/demuxdump.c
+++ b/modules/demux/demuxdump.c
@@ -2,7 +2,6 @@
  * demuxdump.c : Pseudo demux module for vlc (dump raw stream)
  *****************************************************************************
  * Copyright (C) 2001-2004 VLC authors and VideoLAN
- * $Id$
  *
  * Authors: Laurent Aimar <fenrir at via.ecp.fr>
  *
@@ -21,10 +20,6 @@
  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
-/*****************************************************************************
- * Preamble
- *****************************************************************************/
-
 #ifdef HAVE_CONFIG_H
 # include "config.h"
 #endif
@@ -34,9 +29,6 @@
 #include <vlc_demux.h>
 #include <vlc_fs.h>
 
-/*****************************************************************************
- * Module descriptor
- *****************************************************************************/
 #define FILE_TEXT N_("Dump filename")
 #define FILE_LONGTEXT N_( \
     "Name of the file to which the raw stream will be dumped." )
@@ -61,140 +53,89 @@ vlc_module_begin ()
     add_shortcut( "dump" )
 vlc_module_end ()
 
+#define DUMP_BLOCKSIZE  16384
 
-/*****************************************************************************
- * Local prototypes
- *****************************************************************************/
 static int Demux( demux_t * );
 static int Control( demux_t *, int,va_list );
 
-#define DUMP_BLOCKSIZE  16384
-
-struct demux_sys_t
-{
-    char        *psz_file;
-    FILE        *p_file;
-    uint64_t    i_write;
-
-    uint8_t     buffer[DUMP_BLOCKSIZE];
-};
-
-/*
- * Data reading functions
+/**
+ * Initializes the raw dump pseudo-demuxer.
  */
-
-/*****************************************************************************
- * Open: initializes dump structures
- *****************************************************************************/
 static int Open( vlc_object_t * p_this )
 {
-    demux_t     *p_demux = (demux_t*)p_this;
-    demux_sys_t *p_sys;
-    const char  *psz_mode;
-    bool  b_append;
+    demux_t *p_demux = (demux_t*)p_this;
 
     /* Accept only if forced */
     if( !p_demux->b_force )
         return VLC_EGENERIC;
 
-    p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
-    if( !p_sys )
-        return VLC_ENOMEM;
-
-    b_append = var_CreateGetBool( p_demux, "demuxdump-append" );
-    if ( b_append )
-        psz_mode = "ab";
-    else
-        psz_mode = "wb";
-
-    p_demux->pf_demux = Demux;
-    p_demux->pf_control = Control;
+    const char *mode = "wb";
+    if( var_InheritBool( p_demux, "demuxdump-append" ) )
+        mode = "ab";
 
-    p_sys->i_write = 0;
-    p_sys->p_file = NULL;
-    p_sys->psz_file = var_CreateGetString( p_demux, "demuxdump-file" );
-    if( *p_sys->psz_file == '\0' )
-    {
-        msg_Warn( p_demux, "no dump file name given" );
-        free( p_sys->psz_file );
-        free( p_sys );
-        return VLC_EGENERIC;
-    }
+    char *path = var_InheritString( p_demux, "demuxdump-file" );
+    if( path == NULL )
+        return VLC_ENOMEM;
 
-    if( !strcmp( p_sys->psz_file, "-" ) )
+    FILE *stream;
+    if( !strcmp( path, "-" ) )
     {
         msg_Info( p_demux, "dumping raw stream to standard output" );
-        p_sys->p_file = stdout;
+        stream = stdout;
     }
-    else if( ( p_sys->p_file = vlc_fopen( p_sys->psz_file, psz_mode ) ) == NULL )
+    else
     {
-        msg_Err( p_demux, "cannot create `%s' for writing", p_sys->psz_file );
-        free( p_sys->psz_file );
-        free( p_sys );
-        return VLC_EGENERIC;
+        stream = vlc_fopen( path, mode );
+        if( stream == NULL )
+            msg_Err( p_demux, "cannot write `%s': %m", path );
+        else
+            msg_Info( p_demux, "writing raw stream to file `%s'", path );
+        free( path );
+
+        if( stream == NULL )
+            return VLC_EGENERIC;
     }
-    msg_Info( p_demux, "%s raw stream to file `%s'",
-              b_append ? "appending" : "dumping", p_sys->psz_file );
 
+    p_demux->p_sys = (void *)stream;
+    p_demux->pf_demux = Demux;
+    p_demux->pf_control = Control;
     return VLC_SUCCESS;
 }
 
-/*****************************************************************************
- * Close:
- *****************************************************************************/
+/**
+ * Destroys the pseudo-demuxer.
+ */
 static void Close( vlc_object_t *p_this )
 {
+    demux_t *p_demux = (demux_t*)p_this;
+    FILE *stream = (void *)p_demux->p_sys;
 
-    demux_t     *p_demux = (demux_t*)p_this;
-    demux_sys_t *p_sys = p_demux->p_sys;
-
-    msg_Info( p_demux ,"closing %s (%"PRId64" KiB dumped)", p_sys->psz_file,
-              p_sys->i_write / 1024 );
-
-    if( p_sys->p_file != stdout )
-    {
-        fclose( p_sys->p_file );
-    }
-    free( p_sys->psz_file );
-    free( p_sys );
+    if( stream != stdout )
+        fclose( stream );
 }
 
-/*****************************************************************************
- * Demux: reads and demuxes data packets
- *****************************************************************************
- * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
- *****************************************************************************/
+/**
+ * Copy data from input stream to dump file.
+ */
 static int Demux( demux_t *p_demux )
 {
-    demux_sys_t *p_sys = p_demux->p_sys;
-
-    int i_data;
-
-    i_data = stream_Read( p_demux->s, p_sys->buffer, DUMP_BLOCKSIZE );
-    if ( i_data <= 0 )
-        return i_data;
+    FILE *stream = (void *)p_demux->p_sys;
+    char buf[DUMP_BLOCKSIZE];
 
-    i_data = fwrite( p_sys->buffer, 1, i_data, p_sys->p_file );
+    int rd = stream_Read( p_demux->s, buf, sizeof (buf) );
+    if ( rd <= 0 )
+        return rd;
 
-    if( i_data == 0 )
+    size_t wr = fwrite( buf, 1, rd, stream );
+    if( wr != (size_t)rd )
     {
-        msg_Err( p_demux, "failed to write data" );
+        msg_Err( p_demux, "cannot write data: %m" );
         return -1;
     }
-#if 0
-    msg_Dbg( p_demux, "dumped %d bytes", i_data );
-#endif
-
-    p_sys->i_write += i_data;
-
     return 1;
 }
 
-/*****************************************************************************
- * Demux: reads and demuxes data packets
- *****************************************************************************/
 static int Control( demux_t *p_demux, int i_query, va_list args )
 {
     return demux_vaControlHelper( p_demux->s, 0, -1, 0, 1, i_query, args );
 }
-



More information about the vlc-commits mailing list