contrib: cargo: use the 0.6.13 cargo-c version
[vlc.git] / modules / stream_filter / record.c
blobb6bc9151c477f88d21a214914fe1369645448a94
1 /*****************************************************************************
2 * record.c
3 *****************************************************************************
4 * Copyright (C) 2008 Laurent Aimar
6 * Author: Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
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.
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.
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 *****************************************************************************/
23 /*****************************************************************************
24 * Preamble
25 *****************************************************************************/
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
30 #include <vlc_common.h>
31 #include <vlc_plugin.h>
33 #include <assert.h>
34 #include <vlc_stream.h>
35 #include <vlc_input_item.h>
36 #include <vlc_fs.h>
39 /*****************************************************************************
40 * Module descriptor
41 *****************************************************************************/
42 static int Open ( vlc_object_t * );
43 static void Close( vlc_object_t * );
45 vlc_module_begin()
46 set_category( CAT_INPUT )
47 set_subcategory( SUBCAT_INPUT_STREAM_FILTER )
48 set_description( N_("Internal stream record") )
49 set_capability( "stream_filter", 0 )
50 set_callbacks( Open, Close )
51 vlc_module_end()
53 /*****************************************************************************
55 *****************************************************************************/
56 typedef struct
58 FILE *f; /* TODO it could be replaced by access_output_t one day */
59 bool b_error;
60 } stream_sys_t;
63 /****************************************************************************
64 * Local prototypes
65 ****************************************************************************/
66 static ssize_t Read( stream_t *, void *p_read, size_t i_read );
67 static int Seek ( stream_t *, uint64_t );
68 static int Control( stream_t *, int i_query, va_list );
70 static int Start ( stream_t *, const char *psz_extension );
71 static int Stop ( stream_t * );
72 static void Write ( stream_t *, const uint8_t *p_buffer, size_t i_buffer );
74 /****************************************************************************
75 * Open
76 ****************************************************************************/
77 static int Open ( vlc_object_t *p_this )
79 stream_t *s = (stream_t*)p_this;
80 stream_sys_t *p_sys;
82 /* */
83 s->p_sys = p_sys = malloc( sizeof( *p_sys ) );
84 if( !p_sys )
85 return VLC_ENOMEM;
87 p_sys->f = NULL;
89 /* */
90 s->pf_read = Read;
91 s->pf_seek = Seek;
92 s->pf_control = Control;
94 return VLC_SUCCESS;
97 /****************************************************************************
98 * Close
99 ****************************************************************************/
100 static void Close( vlc_object_t *p_this )
102 stream_t *s = (stream_t*)p_this;
103 stream_sys_t *p_sys = s->p_sys;
105 if( p_sys->f )
106 Stop( s );
108 free( p_sys );
111 /****************************************************************************
112 * Stream filters functions
113 ****************************************************************************/
114 static ssize_t Read( stream_t *s, void *p_read, size_t i_read )
116 stream_sys_t *p_sys = s->p_sys;
117 void *p_record = p_read;
118 const ssize_t i_record = vlc_stream_Read( s->s, p_record, i_read );
120 /* Dump read data */
121 if( p_sys->f )
123 if( p_record && i_record > 0 )
124 Write( s, p_record, i_record );
127 return i_record;
130 static int Seek( stream_t *s, uint64_t offset )
132 return vlc_stream_Seek( s->s, offset );
135 static int Control( stream_t *s, int i_query, va_list args )
137 if( i_query != STREAM_SET_RECORD_STATE )
138 return vlc_stream_vaControl( s->s, i_query, args );
140 stream_sys_t *sys = s->p_sys;
141 bool b_active = (bool)va_arg( args, int );
142 const char *psz_extension = NULL;
143 if( b_active )
144 psz_extension = va_arg( args, const char* );
146 if( !sys->f == !b_active )
147 return VLC_SUCCESS;
149 if( b_active )
150 return Start( s, psz_extension );
151 else
152 return Stop( s );
155 /****************************************************************************
156 * Helpers
157 ****************************************************************************/
158 static int Start( stream_t *s, const char *psz_extension )
160 stream_sys_t *p_sys = s->p_sys;
162 char *psz_file;
163 FILE *f;
165 /* */
166 if( !psz_extension )
167 psz_extension = "dat";
169 /* Retreive path */
170 char *psz_path = var_CreateGetNonEmptyString( s, "input-record-path" );
171 if( !psz_path )
172 psz_path = config_GetUserDir( VLC_DOWNLOAD_DIR );
174 if( !psz_path )
175 return VLC_ENOMEM;
177 /* Create file name
178 * TODO allow prefix configuration */
179 psz_file = input_item_CreateFilename( s->p_input_item, psz_path,
180 INPUT_RECORD_PREFIX, psz_extension );
182 free( psz_path );
184 if( !psz_file )
185 return VLC_ENOMEM;
187 f = vlc_fopen( psz_file, "wb" );
188 if( !f )
190 free( psz_file );
191 return VLC_EGENERIC;
194 /* signal new record file */
195 var_SetString( vlc_object_instance(s), "record-file", psz_file );
197 msg_Dbg( s, "Recording into %s", psz_file );
198 free( psz_file );
200 /* */
201 p_sys->f = f;
202 p_sys->b_error = false;
203 return VLC_SUCCESS;
205 static int Stop( stream_t *s )
207 stream_sys_t *p_sys = s->p_sys;
209 assert( p_sys->f );
211 msg_Dbg( s, "Recording completed" );
212 fclose( p_sys->f );
213 p_sys->f = NULL;
214 return VLC_SUCCESS;
217 static void Write( stream_t *s, const uint8_t *p_buffer, size_t i_buffer )
219 stream_sys_t *p_sys = s->p_sys;
221 assert( p_sys->f );
223 if( i_buffer > 0 )
225 const bool b_previous_error = p_sys->b_error;
226 const size_t i_written = fwrite( p_buffer, 1, i_buffer, p_sys->f );
228 p_sys->b_error = i_written != i_buffer;
230 /* TODO maybe a intf_UserError or something like that ? */
231 if( p_sys->b_error && !b_previous_error )
232 msg_Err( s, "Failed to record data (begin)" );
233 else if( !p_sys->b_error && b_previous_error )
234 msg_Err( s, "Failed to record data (end)" );