1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2008 Laurent Aimar
7 * Author: Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
35 #include <vlc_stream.h>
36 #include <vlc_input.h>
37 #include <vlc_charset.h>
40 /*****************************************************************************
42 *****************************************************************************/
43 static int Open ( vlc_object_t * );
44 static void Close( vlc_object_t * );
47 set_category( CAT_INPUT )
48 set_subcategory( SUBCAT_INPUT_STREAM_FILTER )
49 set_description( N_("Internal stream record") )
50 set_capability( "stream_filter", 0 )
51 set_callbacks( Open, Close )
54 /*****************************************************************************
56 *****************************************************************************/
61 FILE *f; /* TODO it could be replaced by access_output_t one day */
66 /****************************************************************************
68 ****************************************************************************/
69 static int Read ( stream_t *, void *p_read, unsigned int i_read );
70 static int Peek ( stream_t *, const uint8_t **pp_peek, unsigned int i_peek );
71 static int Control( stream_t *, int i_query, va_list );
73 static int Start ( stream_t *, const char *psz_extension );
74 static int Stop ( stream_t * );
75 static void Write ( stream_t *, const uint8_t *p_buffer, size_t i_buffer );
77 /****************************************************************************
79 ****************************************************************************/
80 static int Open ( vlc_object_t *p_this )
82 stream_t *s = (stream_t*)p_this;
86 s->p_sys = p_sys = malloc( sizeof( *p_sys ) );
90 p_sys->b_active = false;
95 s->pf_control = Control;
100 /****************************************************************************
102 ****************************************************************************/
103 static void Close( vlc_object_t *p_this )
105 stream_t *s = (stream_t*)p_this;
106 stream_sys_t *p_sys = s->p_sys;
108 if( p_sys->b_active )
114 /****************************************************************************
115 * Stream filters functions
116 ****************************************************************************/
117 static int Read( stream_t *s, void *p_read, unsigned int i_read )
119 stream_sys_t *p_sys = s->p_sys;
120 void *p_record = p_read;
122 /* Allocate a temporary buffer for record when no p_read */
123 if( p_sys->b_active && !p_record )
124 p_record = malloc( i_read );
127 const int i_record = stream_Read( s->p_source, p_record, i_read );
130 if( p_sys->b_active )
132 if( p_record && i_record > 0 )
133 Write( s, p_record, i_record );
141 static int Peek( stream_t *s, const uint8_t **pp_peek, unsigned int i_peek )
143 return stream_Peek( s->p_source, pp_peek, i_peek );
146 static int Control( stream_t *s, int i_query, va_list args )
148 if( i_query != STREAM_SET_RECORD_STATE )
149 return stream_vaControl( s->p_source, i_query, args );
151 bool b_active = (bool)va_arg( args, int );
152 const char *psz_extension = NULL;
154 psz_extension = (const char*)va_arg( args, const char* );
156 if( !s->p_sys->b_active == !b_active )
160 return Start( s, psz_extension );
165 /****************************************************************************
167 ****************************************************************************/
168 static int Start( stream_t *s, const char *psz_extension )
170 stream_sys_t *p_sys = s->p_sys;
177 psz_extension = "dat";
180 char *psz_path = var_CreateGetString( s, "input-record-path" );
181 if( !psz_path || *psz_path == '\0' )
184 psz_path = strdup( config_GetHomeDir() );
191 * TODO allow prefix configuration */
192 psz_file = input_CreateFilename( VLC_OBJECT(s), psz_path, INPUT_RECORD_PREFIX, psz_extension );
199 f = utf8_fopen( psz_file, "wb" );
205 msg_Dbg( s, "Recording into %s", psz_file );
210 p_sys->b_active = true;
211 p_sys->b_error = false;
214 static int Stop( stream_t *s )
216 stream_sys_t *p_sys = s->p_sys;
218 assert( p_sys->b_active );
220 msg_Dbg( s, "Recording completed" );
222 p_sys->b_active = false;
226 static void Write( stream_t *s, const uint8_t *p_buffer, size_t i_buffer )
228 stream_sys_t *p_sys = s->p_sys;
230 assert( p_sys->b_active );
234 const bool b_previous_error = p_sys->b_error;
235 const size_t i_written = fwrite( p_buffer, 1, i_buffer, p_sys->f );
237 p_sys->b_error = i_written != i_buffer;
239 /* TODO maybe a intf_UserError or something like that ? */
240 if( p_sys->b_error && !b_previous_error )
241 msg_Err( s, "Failed to record data (begin)" );
242 else if( !p_sys->b_error && b_previous_error )
243 msg_Err( s, "Failed to record data (end)" );