demuxdump.c 4.48 KB
Newer Older
1 2 3
/*****************************************************************************
 * demuxdump.c : Pseudo demux module for vlc (dump raw stream)
 *****************************************************************************
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
4
 * Copyright (C) 2001-2004 VLC authors and VideoLAN
5 6 7
 *
 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
 *
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
8 9 10
 * 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
11
 * (at your option) any later version.
12
 *
13 14
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
15 16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
17
 *
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
18 19 20
 * 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.
21 22
 *****************************************************************************/

23 24 25 26
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

27
#include <vlc_common.h>
28
#include <vlc_plugin.h>
29
#include <vlc_demux.h>
30
#include <vlc_sout.h>
31

32
#define ACCESS_TEXT N_("Dump module")
Clément Stenac's avatar
Clément Stenac committed
33
#define FILE_TEXT N_("Dump filename")
34
#define FILE_LONGTEXT N_( \
Clément Stenac's avatar
Clément Stenac committed
35 36
    "Name of the file to which the raw stream will be dumped." )
#define APPEND_TEXT N_("Append to existing file")
37
#define APPEND_LONGTEXT N_( \
Clément Stenac's avatar
Clément Stenac committed
38
    "If the file already exists, it will not be overwritten." )
39

40 41 42
static int  Open( vlc_object_t * );
static void Close ( vlc_object_t * );

43 44 45 46 47 48
vlc_module_begin ()
    set_shortname("Dump")
    set_category( CAT_INPUT )
    set_subcategory( SUBCAT_INPUT_DEMUX )
    set_description( N_("File dumper") )
    set_capability( "demux", 0 )
49 50
    add_module( "demuxdump-access", "sout access", "file", ACCESS_TEXT,
                ACCESS_TEXT, true )
51
    add_savefile( "demuxdump-file", "stream-demux.dump", FILE_TEXT,
52
                  FILE_LONGTEXT, false )
53
    add_bool( "demuxdump-append", false, APPEND_TEXT, APPEND_LONGTEXT,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
54
              false )
55 56 57
    set_callbacks( Open, Close )
    add_shortcut( "dump" )
vlc_module_end ()
58

59
#define DUMP_BLOCKSIZE  16384
60 61 62 63

static int Demux( demux_t * );
static int Control( demux_t *, int,va_list );

64 65
/**
 * Initializes the raw dump pseudo-demuxer.
66
 */
67
static int Open( vlc_object_t * p_this )
68
{
69
    demux_t *p_demux = (demux_t*)p_this;
70

71
    /* Accept only if forced */
72
    if( !p_demux->obj.force )
73
        return VLC_EGENERIC;
74

75 76 77 78 79 80
    char *access = var_InheritString( p_demux, "demuxdump-access" );
    if( access == NULL )
        return VLC_EGENERIC;

    /* --sout-file-append (defaults to false) */
    var_Create( p_demux, "sout-file-append", VLC_VAR_BOOL );
81
    if( var_InheritBool( p_demux, "demuxdump-append" ) )
82 83 84
        var_SetBool( p_demux, "sout-file-append", true );
    /* --sout-file-format (always false) */
    var_Create( p_demux, "sout-file-format", VLC_VAR_BOOL );
85

86 87
    char *path = var_InheritString( p_demux, "demuxdump-file" );
    if( path == NULL )
88
    {
89 90 91
        free( access );
        msg_Err( p_demux, "no dump file name given" );
        return VLC_EGENERIC;
92
    }
93 94 95 96 97

    sout_access_out_t *out = sout_AccessOutNew( p_demux, access, path );
    free( path );
    free( access );
    if( out == NULL )
98
    {
99 100
        msg_Err( p_demux, "cannot create output" );
        return VLC_EGENERIC;
101 102
    }

103
    p_demux->p_sys = (void *)out;
104 105
    p_demux->pf_demux = Demux;
    p_demux->pf_control = Control;
106
    return VLC_SUCCESS;
107 108
}

109 110 111
/**
 * Destroys the pseudo-demuxer.
 */
112
static void Close( vlc_object_t *p_this )
113
{
114
    demux_t *p_demux = (demux_t*)p_this;
115
    sout_access_out_t *out = (void *)p_demux->p_sys;
116

117
    sout_AccessOutDelete( out );
118 119
}

120 121 122
/**
 * Copy data from input stream to dump file.
 */
123
static int Demux( demux_t *p_demux )
124
{
125 126 127 128 129
    sout_access_out_t *out = (void *)p_demux->p_sys;

    block_t *block = block_Alloc( DUMP_BLOCKSIZE );
    if( unlikely(block == NULL) )
        return -1;
130

131
    int rd = vlc_stream_Read( p_demux->s, block->p_buffer, DUMP_BLOCKSIZE );
132
    if ( rd <= 0 )
133 134
    {
        block_Release( block );
135
        return rd;
136 137
    }
    block->i_buffer = rd;
138

139
    size_t wr = sout_AccessOutWrite( out, block );
140
    if( wr != (size_t)rd )
141
    {
142
        msg_Err( p_demux, "cannot write data" );
143
        return -1;
144
    }
145 146
    return 1;
}
147

148 149
static int Control( demux_t *p_demux, int i_query, va_list args )
{
150
    return demux_vaControlHelper( p_demux->s, 0, -1, 0, 1, i_query, args );
151
}