]> git.sesse.net Git - vlc/blob - modules/demux/demuxdump.c
mediacodec: don't loop in GetOutput
[vlc] / modules / demux / demuxdump.c
1 /*****************************************************************************
2  * demuxdump.c : Pseudo demux module for vlc (dump raw stream)
3  *****************************************************************************
4  * Copyright (C) 2001-2004 VLC authors and VideoLAN
5  *
6  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU Lesser General Public License as published by
10  * the Free Software Foundation; either version 2.1 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21  *****************************************************************************/
22
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif
26
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")
33 #define FILE_TEXT N_("Dump filename")
34 #define FILE_LONGTEXT N_( \
35     "Name of the file to which the raw stream will be dumped." )
36 #define APPEND_TEXT N_("Append to existing file")
37 #define APPEND_LONGTEXT N_( \
38     "If the file already exists, it will not be overwritten." )
39
40 static int  Open( vlc_object_t * );
41 static void Close ( vlc_object_t * );
42
43 vlc_module_begin ()
44     set_shortname("Dump")
45     set_category( CAT_INPUT )
46     set_subcategory( SUBCAT_INPUT_DEMUX )
47     set_description( N_("File dumper") )
48     set_capability( "demux", 0 )
49     add_module( "demuxdump-access", "sout access", "file", ACCESS_TEXT,
50                 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,
54               false )
55     set_callbacks( Open, Close )
56     add_shortcut( "dump" )
57 vlc_module_end ()
58
59 #define DUMP_BLOCKSIZE  16384
60
61 static int Demux( demux_t * );
62 static int Control( demux_t *, int,va_list );
63
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->b_force )
73         return VLC_EGENERIC;
74
75     char *access = var_InheritString( p_demux, "demuxdump-access" );
76     if( access == NULL )
77         return VLC_EGENERIC;
78
79     /* --sout-file-append (defaults to false) */
80     var_Create( p_demux, "sout-file-append", VLC_VAR_BOOL );
81     if( var_InheritBool( p_demux, "demuxdump-append" ) )
82         var_SetBool( p_demux, "sout-file-append", true );
83     /* --sout-file-format (always false) */
84     var_Create( p_demux, "sout-file-format", VLC_VAR_BOOL );
85
86     char *path = var_InheritString( p_demux, "demuxdump-file" );
87     if( path == NULL )
88     {
89         free( access );
90         msg_Err( p_demux, "no dump file name given" );
91         return VLC_EGENERIC;
92     }
93
94     sout_access_out_t *out = sout_AccessOutNew( p_demux, access, path );
95     free( path );
96     free( access );
97     if( out == NULL )
98     {
99         msg_Err( p_demux, "cannot create output" );
100         return VLC_EGENERIC;
101     }
102
103     p_demux->p_sys = (void *)out;
104     p_demux->pf_demux = Demux;
105     p_demux->pf_control = Control;
106     return VLC_SUCCESS;
107 }
108
109 /**
110  * Destroys the pseudo-demuxer.
111  */
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  * Copy data from input stream to dump file.
122  */
123 static int Demux( demux_t *p_demux )
124 {
125     sout_access_out_t *out = (void *)p_demux->p_sys;
126
127     block_t *block = block_Alloc( DUMP_BLOCKSIZE );
128     if( unlikely(block == NULL) )
129         return -1;
130
131     int rd = 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     return 1;
146 }
147
148 static int Control( demux_t *p_demux, int i_query, va_list args )
149 {
150     return demux_vaControlHelper( p_demux->s, 0, -1, 0, 1, i_query, args );
151 }