]> git.sesse.net Git - vlc/blobdiff - modules/demux/demuxdump.c
demux: ts: flush queues after seek
[vlc] / modules / demux / demuxdump.c
index 7dae59cae2155724eb6ea3771bf598d0f5d5b820..5c12244eaa914d37363ed9c0285f62b34ea3ce8c 100644 (file)
 /*****************************************************************************
  * demuxdump.c : Pseudo demux module for vlc (dump raw stream)
  *****************************************************************************
- * Copyright (C) 2001 VideoLAN
- * $Id: demuxdump.c,v 1.3 2003/01/25 16:58:34 fenrir Exp $
+ * Copyright (C) 2001-2004 VLC authors and VideoLAN
  *
  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
  * (at your option) any later version.
- * 
+ *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
  *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
- *****************************************************************************/
-
-/*****************************************************************************
- * Preamble
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
-#include <stdlib.h>                                      /* malloc(), free() */
-#include <string.h>                                              /* strdup() */
-#include <errno.h>
 
-#include <vlc/vlc.h>
-#include <vlc/input.h>
-
-#include <sys/types.h>
-
-/*****************************************************************************
- * Local prototypes
- *****************************************************************************/
-static int  Activate ( vlc_object_t * );
-static int  Demux ( input_thread_t * );
-static void Desactivate ( vlc_object_t * );
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <vlc_common.h>
+#include <vlc_plugin.h>
+#include <vlc_demux.h>
+#include <vlc_sout.h>
+
+#define ACCESS_TEXT N_("Dump module")
+#define FILE_TEXT N_("Dump filename")
+#define FILE_LONGTEXT N_( \
+    "Name of the file to which the raw stream will be dumped." )
+#define APPEND_TEXT N_("Append to existing file")
+#define APPEND_LONGTEXT N_( \
+    "If the file already exists, it will not be overwritten." )
+
+static int  Open( vlc_object_t * );
+static void Close ( vlc_object_t * );
+
+vlc_module_begin ()
+    set_shortname("Dump")
+    set_category( CAT_INPUT )
+    set_subcategory( SUBCAT_INPUT_DEMUX )
+    set_description( N_("File dumper") )
+    set_capability( "demux", 0 )
+    add_module( "demuxdump-access", "sout access", "file", ACCESS_TEXT,
+                ACCESS_TEXT, true )
+    add_savefile( "demuxdump-file", "stream-demux.dump", FILE_TEXT,
+                  FILE_LONGTEXT, false )
+    add_bool( "demuxdump-append", false, APPEND_TEXT, APPEND_LONGTEXT,
+              false )
+    set_callbacks( Open, Close )
+    add_shortcut( "dump" )
+vlc_module_end ()
 
 #define DUMP_BLOCKSIZE  16384
 
-/*****************************************************************************
- * Module descriptor
- *****************************************************************************/
-vlc_module_begin();
-    set_description( _("Dump Demux input") );
-    set_capability( "demux", 0 );
-    add_category_hint( "File", NULL );
-        add_string( "demuxdump-file", NULL, NULL, 
-                    "dump file name", 
-                    "file name for dumping raw stream read by demux" );
-    set_callbacks( Activate, Desactivate );
-    add_shortcut( "dump" );
-vlc_module_end();
-
-struct demux_sys_t
-{
-    char        *psz_name;
-    FILE        *p_file;
-    uint64_t    i_write;
-
-    void        *p_demux_data_sav;
-};
+static int Demux( demux_t * );
+static int Control( demux_t *, int,va_list );
 
-/*
- * Data reading functions
+/**
+ * Initializes the raw dump pseudo-demuxer.
  */
-
-/*****************************************************************************
- * Activate: initializes dump structures
- *****************************************************************************/
-static int Activate( vlc_object_t * p_this )
+static int Open( vlc_object_t * p_this )
 {
-    input_thread_t      *p_input = (input_thread_t *)p_this;
-    demux_sys_t         *p_demux;
-
-    char                *psz_name;
+    demux_t *p_demux = (demux_t*)p_this;
 
-    /* Set the demux function */
-    p_input->pf_demux = Demux;
+    /* Accept only if forced */
+    if( !p_demux->b_force )
+        return VLC_EGENERIC;
 
-    /* Initialize access plug-in structures. */
-    if( p_input->i_mtu == 0 )
-    {
-        /* Improve speed. */
-        p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
-    }
-    
-    psz_name = config_GetPsz( p_input, "demuxdump-file" );
-    if( !psz_name || !*psz_name )
-    {
-        psz_name = strdup( "stream-demux.dump" );
-    }
+    char *access = var_InheritString( p_demux, "demuxdump-access" );
+    if( access == NULL )
+        return VLC_EGENERIC;
 
-    p_demux = malloc( sizeof( demux_sys_t ) );
-    memset( p_demux, 0, sizeof( demux_sys_t ) );
+    /* --sout-file-append (defaults to false) */
+    var_Create( p_demux, "sout-file-append", VLC_VAR_BOOL );
+    if( var_InheritBool( p_demux, "demuxdump-append" ) )
+        var_SetBool( p_demux, "sout-file-append", true );
+    /* --sout-file-format (always false) */
+    var_Create( p_demux, "sout-file-format", VLC_VAR_BOOL );
 
-    if( !( p_demux->p_file = fopen( psz_name, "wb" ) ) )
-    {
-        msg_Err( p_input,
-                 "cannot create `%s' for writing", 
-                 psz_name );
-        free( p_demux );
-        return( -1 );
-    }
-    else
+    char *path = var_InheritString( p_demux, "demuxdump-file" );
+    if( path == NULL )
     {
-        msg_Info( p_input,
-                  "dumping raw stream to file `%s'", 
-                  psz_name );
-        p_demux->psz_name = psz_name;
+        free( access );
+        msg_Err( p_demux, "no dump file name given" );
+        return VLC_EGENERIC;
     }
 
-    p_demux->i_write = 0;
-    p_demux->p_demux_data_sav = p_input->p_demux_data;
-
-    if( p_input->stream.p_selected_program != NULL )
+    sout_access_out_t *out = sout_AccessOutNew( p_demux, access, path );
+    free( path );
+    free( access );
+    if( out == NULL )
     {
-        /* workaround for dvd access */
-        msg_Warn( p_input, "demux data already initializated (by access?)" );
+        msg_Err( p_demux, "cannot create output" );
+        return VLC_EGENERIC;
     }
-    else
-    {
 
-        if( input_InitStream( p_input, 0 ) == -1 )
-        {
-            fclose( p_demux->p_file );
-            free( p_demux );
-            return( -1 );
-        }
-        input_AddProgram( p_input, 0, 0 );
-        p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
-
-        vlc_mutex_lock( &p_input->stream.stream_lock );
-        p_input->stream.p_selected_area->i_tell = 0;
-        vlc_mutex_unlock( &p_input->stream.stream_lock );
-    }
-
-    p_input->p_demux_data = p_demux;
-
-    vlc_mutex_lock( &p_input->stream.stream_lock );
-    p_input->stream.p_selected_program->b_is_ok = 1;
-    vlc_mutex_unlock( &p_input->stream.stream_lock );
-    
-    return( 0 );
+    p_demux->p_sys = (void *)out;
+    p_demux->pf_demux = Demux;
+    p_demux->pf_control = Control;
+    return VLC_SUCCESS;
 }
 
-/*****************************************************************************
- * Desctivate: initializes dump structures
- *****************************************************************************/
-static void Desactivate ( vlc_object_t *p_this )
+/**
+ * Destroys the pseudo-demuxer.
+ */
+static void Close( vlc_object_t *p_this )
 {
-    input_thread_t      *p_input = (input_thread_t *)p_this;
-    demux_sys_t         *p_demux = (demux_sys_t*)p_input->p_demux_data;
-
-    msg_Info( p_input,
-              "closing %s ("I64Fd" Kbytes dumped)",
-              p_demux->psz_name,
-              p_demux->i_write / 1024 );
+    demux_t *p_demux = (demux_t*)p_this;
+    sout_access_out_t *out = (void *)p_demux->p_sys;
 
-    if( p_demux->p_file )
-    {
-        fclose( p_demux->p_file );
-        p_demux->p_file = NULL;
-    }
-    if( p_demux->psz_name )
-    {
-        free( p_demux->psz_name );
-    }
-    p_input->p_demux_data = p_demux->p_demux_data_sav;
-    free( p_demux );
+    sout_AccessOutDelete( out );
 }
 
-/*****************************************************************************
- * Demux: reads and demuxes data packets
- *****************************************************************************
- * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
- *****************************************************************************/
-static int Demux( input_thread_t * p_input )
+/**
+ * Copy data from input stream to dump file.
+ */
+static int Demux( demux_t *p_demux )
 {
-    demux_sys_t         *p_demux = (demux_sys_t*)p_input->p_demux_data;
-
-    ssize_t         i_read;
-    data_packet_t * p_data;
-    int             i_write;
-
-    p_input->p_demux_data = p_demux->p_demux_data_sav;
-    i_read = input_SplitBuffer( p_input, &p_data, DUMP_BLOCKSIZE );
-    p_input->p_demux_data = p_demux;
-    
-    if ( i_read <= 0 )
-    {
-        return i_read;
-    }
+    sout_access_out_t *out = (void *)p_demux->p_sys;
 
-    i_write = fwrite( p_data->p_payload_start,
-                       1,
-                       i_read,
-                       p_demux->p_file );
-    
-    input_DeletePacket( p_input->p_method_data, p_data );
+    block_t *block = block_Alloc( DUMP_BLOCKSIZE );
+    if( unlikely(block == NULL) )
+        return -1;
 
-    if( i_write < 0 )
+    int rd = stream_Read( p_demux->s, block->p_buffer, DUMP_BLOCKSIZE );
+    if ( rd <= 0 )
     {
-        msg_Err( p_input, 
-                 "failed to write %d bytes",
-                 i_write );
-        return( -1 );
+        block_Release( block );
+        return rd;
     }
-    else
-    {
-        msg_Dbg( p_input,
-                 "dumped %d bytes",
-                 i_write );
-        p_demux->i_write += i_write;
-    }
-
+    block->i_buffer = rd;
 
-    if( (p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_REINIT)
-         | (input_ClockManageControl( p_input, 
-                      p_input->stream.p_selected_program,
-                         (mtime_t)0 ) == PAUSE_S) )
+    size_t wr = sout_AccessOutWrite( out, block );
+    if( wr != (size_t)rd )
     {
-        msg_Warn( p_input, "synchro reinit" );
-        p_input->stream.p_selected_program->i_synchro_state = SYNCHRO_OK;
+        msg_Err( p_demux, "cannot write data" );
+        return -1;
     }
-
-    return( 1 );
+    return 1;
 }
 
+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 );
+}