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 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 /*****************************************************************************
26 *****************************************************************************/
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
35 #include <vlc_stream.h>
36 #include <vlc_input.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 *****************************************************************************/
59 FILE *f
; /* TODO it could be replaced by access_output_t one day */
64 /****************************************************************************
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 /****************************************************************************
77 ****************************************************************************/
78 static int Open ( vlc_object_t
*p_this
)
80 stream_t
*s
= (stream_t
*)p_this
;
84 s
->p_sys
= p_sys
= malloc( sizeof( *p_sys
) );
93 s
->pf_control
= Control
;
94 stream_FilterSetDefaultReadDir( s
);
99 /****************************************************************************
101 ****************************************************************************/
102 static void Close( vlc_object_t
*p_this
)
104 stream_t
*s
= (stream_t
*)p_this
;
105 stream_sys_t
*p_sys
= s
->p_sys
;
113 /****************************************************************************
114 * Stream filters functions
115 ****************************************************************************/
116 static ssize_t
Read( stream_t
*s
, void *p_read
, size_t i_read
)
118 stream_sys_t
*p_sys
= s
->p_sys
;
119 void *p_record
= p_read
;
120 const ssize_t i_record
= vlc_stream_Read( s
->s
, p_record
, i_read
);
125 if( p_record
&& i_record
> 0 )
126 Write( s
, p_record
, i_record
);
132 static int Seek( stream_t
*s
, uint64_t offset
)
134 return vlc_stream_Seek( s
->s
, offset
);
137 static int Control( stream_t
*s
, int i_query
, va_list args
)
139 if( i_query
!= STREAM_SET_RECORD_STATE
)
140 return vlc_stream_vaControl( s
->s
, i_query
, args
);
142 stream_sys_t
*sys
= s
->p_sys
;
143 bool b_active
= (bool)va_arg( args
, int );
144 const char *psz_extension
= NULL
;
146 psz_extension
= va_arg( args
, const char* );
148 if( !sys
->f
== !b_active
)
152 return Start( s
, psz_extension
);
157 /****************************************************************************
159 ****************************************************************************/
160 static int Start( stream_t
*s
, const char *psz_extension
)
162 stream_sys_t
*p_sys
= s
->p_sys
;
169 psz_extension
= "dat";
172 char *psz_path
= var_CreateGetNonEmptyString( s
, "input-record-path" );
174 psz_path
= config_GetUserDir( VLC_DOWNLOAD_DIR
);
180 * TODO allow prefix configuration */
181 psz_file
= input_CreateFilename( s
->p_input
, psz_path
, INPUT_RECORD_PREFIX
, psz_extension
);
188 f
= vlc_fopen( psz_file
, "wb" );
195 /* signal new record file */
196 var_SetString( s
->obj
.libvlc
, "record-file", psz_file
);
198 msg_Dbg( s
, "Recording into %s", psz_file
);
203 p_sys
->b_error
= false;
206 static int Stop( stream_t
*s
)
208 stream_sys_t
*p_sys
= s
->p_sys
;
212 msg_Dbg( s
, "Recording completed" );
218 static void Write( stream_t
*s
, const uint8_t *p_buffer
, size_t i_buffer
)
220 stream_sys_t
*p_sys
= s
->p_sys
;
226 const bool b_previous_error
= p_sys
->b_error
;
227 const size_t i_written
= fwrite( p_buffer
, 1, i_buffer
, p_sys
->f
);
229 p_sys
->b_error
= i_written
!= i_buffer
;
231 /* TODO maybe a intf_UserError or something like that ? */
232 if( p_sys
->b_error
&& !b_previous_error
)
233 msg_Err( s
, "Failed to record data (begin)" );
234 else if( !p_sys
->b_error
&& b_previous_error
)
235 msg_Err( s
, "Failed to record data (end)" );