input: add an input_item_t arg to input_CreateFilename()
[vlc.git] / modules / stream_filter / record.c
blobd35fba73a9800ecab7a3c568072fca357b14824e
1 /*****************************************************************************
2 * record.c
3 *****************************************************************************
4 * Copyright (C) 2008 Laurent Aimar
5 * $Id$
7 * Author: Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
25 * Preamble
26 *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
34 #include <assert.h>
35 #include <vlc_stream.h>
36 #include <vlc_input.h>
37 #include <vlc_fs.h>
40 /*****************************************************************************
41 * Module descriptor
42 *****************************************************************************/
43 static int Open ( vlc_object_t * );
44 static void Close( vlc_object_t * );
46 vlc_module_begin()
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 )
52 vlc_module_end()
54 /*****************************************************************************
56 *****************************************************************************/
57 typedef struct
59 FILE *f; /* TODO it could be replaced by access_output_t one day */
60 bool b_error;
61 } stream_sys_t;
64 /****************************************************************************
65 * Local prototypes
66 ****************************************************************************/
67 static ssize_t Read( stream_t *, void *p_read, size_t i_read );
68 static int Seek ( stream_t *, uint64_t );
69 static int Control( stream_t *, int i_query, va_list );
71 static int Start ( stream_t *, const char *psz_extension );
72 static int Stop ( stream_t * );
73 static void Write ( stream_t *, const uint8_t *p_buffer, size_t i_buffer );
75 /****************************************************************************
76 * Open
77 ****************************************************************************/
78 static int Open ( vlc_object_t *p_this )
80 stream_t *s = (stream_t*)p_this;
81 stream_sys_t *p_sys;
83 if( s->s->pf_readdir != NULL )
84 return VLC_EGENERIC;
86 /* */
87 s->p_sys = p_sys = malloc( sizeof( *p_sys ) );
88 if( !p_sys )
89 return VLC_ENOMEM;
91 p_sys->f = NULL;
93 /* */
94 s->pf_read = Read;
95 s->pf_seek = Seek;
96 s->pf_control = Control;
98 return VLC_SUCCESS;
101 /****************************************************************************
102 * Close
103 ****************************************************************************/
104 static void Close( vlc_object_t *p_this )
106 stream_t *s = (stream_t*)p_this;
107 stream_sys_t *p_sys = s->p_sys;
109 if( p_sys->f )
110 Stop( s );
112 free( p_sys );
115 /****************************************************************************
116 * Stream filters functions
117 ****************************************************************************/
118 static ssize_t Read( stream_t *s, void *p_read, size_t i_read )
120 stream_sys_t *p_sys = s->p_sys;
121 void *p_record = p_read;
122 const ssize_t i_record = vlc_stream_Read( s->s, p_record, i_read );
124 /* Dump read data */
125 if( p_sys->f )
127 if( p_record && i_record > 0 )
128 Write( s, p_record, i_record );
131 return i_record;
134 static int Seek( stream_t *s, uint64_t offset )
136 return vlc_stream_Seek( s->s, offset );
139 static int Control( stream_t *s, int i_query, va_list args )
141 if( i_query != STREAM_SET_RECORD_STATE )
142 return vlc_stream_vaControl( s->s, i_query, args );
144 stream_sys_t *sys = s->p_sys;
145 bool b_active = (bool)va_arg( args, int );
146 const char *psz_extension = NULL;
147 if( b_active )
148 psz_extension = va_arg( args, const char* );
150 if( !sys->f == !b_active )
151 return VLC_SUCCESS;
153 if( b_active )
154 return Start( s, psz_extension );
155 else
156 return Stop( s );
159 /****************************************************************************
160 * Helpers
161 ****************************************************************************/
162 static int Start( stream_t *s, const char *psz_extension )
164 stream_sys_t *p_sys = s->p_sys;
166 char *psz_file;
167 FILE *f;
169 /* */
170 if( !psz_extension )
171 psz_extension = "dat";
173 /* Retreive path */
174 char *psz_path = var_CreateGetNonEmptyString( s, "input-record-path" );
175 if( !psz_path )
176 psz_path = config_GetUserDir( VLC_DOWNLOAD_DIR );
178 if( !psz_path )
179 return VLC_ENOMEM;
181 /* Create file name
182 * TODO allow prefix configuration */
183 psz_file = input_CreateFilename( s->p_input, NULL, psz_path, INPUT_RECORD_PREFIX, psz_extension );
185 free( psz_path );
187 if( !psz_file )
188 return VLC_ENOMEM;
190 f = vlc_fopen( psz_file, "wb" );
191 if( !f )
193 free( psz_file );
194 return VLC_EGENERIC;
197 /* signal new record file */
198 var_SetString( s->obj.libvlc, "record-file", psz_file );
200 msg_Dbg( s, "Recording into %s", psz_file );
201 free( psz_file );
203 /* */
204 p_sys->f = f;
205 p_sys->b_error = false;
206 return VLC_SUCCESS;
208 static int Stop( stream_t *s )
210 stream_sys_t *p_sys = s->p_sys;
212 assert( p_sys->f );
214 msg_Dbg( s, "Recording completed" );
215 fclose( p_sys->f );
216 p_sys->f = NULL;
217 return VLC_SUCCESS;
220 static void Write( stream_t *s, const uint8_t *p_buffer, size_t i_buffer )
222 stream_sys_t *p_sys = s->p_sys;
224 assert( p_sys->f );
226 if( i_buffer > 0 )
228 const bool b_previous_error = p_sys->b_error;
229 const size_t i_written = fwrite( p_buffer, 1, i_buffer, p_sys->f );
231 p_sys->b_error = i_written != i_buffer;
233 /* TODO maybe a intf_UserError or something like that ? */
234 if( p_sys->b_error && !b_previous_error )
235 msg_Err( s, "Failed to record data (begin)" );
236 else if( !p_sys->b_error && b_previous_error )
237 msg_Err( s, "Failed to record data (end)" );