1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2005-2006 the VideoLAN team
7 * Author: Laurent Aimar <fenrir@via.ecp.fr>
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 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
35 #include <vlc_input.h>
36 #include <vlc_access.h>
40 #include <vlc_charset.h>
44 /*****************************************************************************
46 *****************************************************************************/
48 #define RECORD_PATH_TXT N_("Record directory")
49 #define RECORD_PATH_LONGTXT N_( \
50 "Directory where the record will be stored." )
52 static int Open ( vlc_object_t * );
53 static void Close( vlc_object_t * );
56 set_shortname( N_("Record") );
57 set_description( N_("Record") );
58 set_category( CAT_INPUT );
59 set_subcategory( SUBCAT_INPUT_ACCESS_FILTER );
60 set_capability( "access_filter", 0 );
61 add_shortcut( "record" );
63 add_directory( "record-path", NULL, NULL,
64 RECORD_PATH_TXT, RECORD_PATH_LONGTXT, true );
67 set_callbacks( Open, Close );
71 /*****************************************************************************
73 *****************************************************************************/
75 static block_t *Block ( access_t * );
76 static ssize_t Read ( access_t *, uint8_t *, size_t );
77 static int Control( access_t *, int i_query, va_list args );
78 static int Seek ( access_t *, int64_t );
80 static void Dump( access_t *, uint8_t *, int );
82 static int EventKey( vlc_object_t *, char const *,
83 vlc_value_t, vlc_value_t, void * );
95 vout_thread_t *p_vout;
101 static inline void PreUpdateFlags( access_t *p_access )
103 access_t *p_src = p_access->p_source;
104 /* backport flags turned off 0 */
105 p_src->info.i_update &= p_access->p_sys->i_update_sav ^ (~p_access->info.i_update);
108 static inline void PostUpdateFlags( access_t *p_access )
110 access_t *p_src = p_access->p_source;
112 p_access->info = p_src->info;
113 p_access->p_sys->i_update_sav = p_access->info.i_update;
117 /*****************************************************************************
119 *****************************************************************************/
120 static int Open( vlc_object_t *p_this )
122 access_t *p_access = (access_t*)p_this;
123 access_t *p_src = p_access->p_source;
127 p_access->pf_read = p_src->pf_read ? Read : NULL;
128 p_access->pf_block = p_src->pf_block ? Block : NULL;
129 p_access->pf_seek = p_src->pf_seek ? Seek : NULL;
130 p_access->pf_control = Control;
132 p_access->info = p_src->info;
134 p_access->p_sys = p_sys = malloc( sizeof( access_t ) );
135 if( !p_sys ) return VLC_ENOMEM;
140 p_sys->psz_file = NULL;
141 p_sys->psz_ext = "dat";
142 p_sys->b_dump = false;
143 p_sys->p_vout = NULL;
144 p_sys->i_vout_chan = -1;
145 p_sys->i_update_sav = p_access->info.i_update;
147 if( !strncasecmp( p_src->psz_access, "dvb", 3 ) ||
148 !strncasecmp( p_src->psz_access, "udp", 3 ) )
149 p_sys->psz_ext = "ts";
151 psz = var_CreateGetString( p_access, "record-path" );
155 psz = strdup( config_GetHomeDir() );
157 p_sys->psz_path = psz;
158 msg_Dbg( p_access, "Record access filter path %s", psz );
160 /* catch all key event */
161 var_AddCallback( p_access->p_libvlc, "key-action", EventKey, p_access );
166 /*****************************************************************************
168 *****************************************************************************/
169 static void Close( vlc_object_t *p_this )
171 access_t *p_access = (access_t*)p_this;
172 access_sys_t *p_sys = p_access->p_sys;
174 var_DelCallback( p_access->p_libvlc, "key-action", EventKey, p_access );
179 free( p_sys->psz_file );
182 free( p_sys->psz_path );
186 /*****************************************************************************
188 *****************************************************************************/
189 static block_t *Block( access_t *p_access )
191 access_t *p_src = p_access->p_source;
194 PreUpdateFlags( p_access );
196 p_block = p_src->pf_block( p_src );
197 if( p_block && p_block->i_buffer )
198 Dump( p_access, p_block->p_buffer, p_block->i_buffer );
200 PostUpdateFlags( p_access );
205 /*****************************************************************************
207 *****************************************************************************/
208 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
210 access_t *p_src = p_access->p_source;
213 PreUpdateFlags( p_access );
215 i_ret = p_src->pf_read( p_src, p_buffer, i_len );
217 Dump( p_access, p_buffer, i_ret );
219 PostUpdateFlags( p_access );
224 /*****************************************************************************
226 *****************************************************************************/
227 static int Control( access_t *p_access, int i_query, va_list args )
229 access_t *p_src = p_access->p_source;
232 PreUpdateFlags( p_access );
234 i_ret = p_src->pf_control( p_src, i_query, args );
236 PostUpdateFlags( p_access );
241 /*****************************************************************************
243 *****************************************************************************/
244 static int Seek( access_t *p_access, int64_t i_pos )
246 access_t *p_src = p_access->p_source;
249 PreUpdateFlags( p_access );
251 i_ret = p_src->pf_seek( p_src, i_pos );
253 PostUpdateFlags( p_access );
258 /*****************************************************************************
260 *****************************************************************************/
261 static int EventKey( vlc_object_t *p_this, char const *psz_var,
262 vlc_value_t oldval, vlc_value_t newval, void *p_data )
264 access_t *p_access = p_data;
265 access_sys_t *p_sys = p_access->p_sys;
270 if( newval.i_int == ACTIONID_RECORD )
273 p_sys->b_dump = false;
275 p_sys->b_dump = true;
281 /*****************************************************************************
283 *****************************************************************************/
284 static void Notify( access_t *p_access, bool b_dump )
286 access_sys_t *p_sys = p_access->p_sys;
287 vout_thread_t *p_vout;
289 p_vout = vlc_object_find( p_access, VLC_OBJECT_VOUT, FIND_ANYWHERE );
290 if( !p_vout ) return;
292 if( p_vout != p_sys->p_vout )
294 p_sys->p_vout = p_vout;
295 if( spu_Control( p_vout->p_spu, SPU_CHANNEL_REGISTER,
296 &p_sys->i_vout_chan ) )
297 p_sys->i_vout_chan = -1;
300 if( p_sys->i_vout_chan != -1 )
303 vout_OSDMessage( p_vout, p_sys->i_vout_chan, _("Recording") );
305 vout_OSDMessage( p_vout, p_sys->i_vout_chan, _("Recording done") );
307 vlc_object_release( p_vout );
310 /*****************************************************************************
312 *****************************************************************************/
313 static void Dump( access_t *p_access, uint8_t *p_buffer, int i_buffer )
315 access_sys_t *p_sys = p_access->p_sys;
322 msg_Dbg( p_access, "dumped %"PRId64" kb (%s)",
323 p_sys->i_size/1024, p_sys->psz_file );
325 Notify( p_access, false );
330 free( p_sys->psz_file );
331 p_sys->psz_file = NULL;
340 input_thread_t *p_input;
341 char *psz_name = NULL, *psz;
342 time_t t = time(NULL);
345 if( !localtime_r( &t, &l ) ) memset( &l, 0, sizeof(l) );
347 p_input = vlc_object_find( p_access, VLC_OBJECT_INPUT, FIND_PARENT );
350 input_item_t * p_item = input_GetItem( p_input );
351 vlc_mutex_lock( &p_item->lock );
352 if( p_item->psz_name )
354 char *p = strrchr( p_item->psz_name, '/' );
356 p = strrchr( p_item->psz_name, '\\' );
359 psz_name = strdup( p_item->psz_name );
360 else if( p[1] != '\0' )
361 psz_name = strdup( &p[1] );
363 vlc_mutex_unlock( &p_item->lock );
365 vlc_object_release( p_input );
368 if( asprintf( &p_sys->psz_file, "%s %d-%d-%d %.2dh%.2dm%.2ds.%s",
369 ( psz_name != NULL ) ? psz_name : "Unknown",
370 l.tm_mday, l.tm_mon+1, l.tm_year+1900,
371 l.tm_hour, l.tm_min, l.tm_sec,
372 p_sys->psz_ext ) == -1 )
373 p_sys->psz_file = NULL;
376 if( p_sys->psz_file == NULL )
378 p_sys->b_dump = false;
382 /* Remove all forbidden characters (except (back)slashes) */
383 for( psz = p_sys->psz_file; *psz; psz++ )
385 unsigned char c = (unsigned char)*psz;
387 /* Even if many OS accept non printable characters, we remove
388 * them to avoid confusing users */
389 if( ( c < 32 ) || ( c == 127 ) )
391 #if defined (WIN32) || defined (UNDER_CE)
392 /* Windows has a lot of forbidden characters, even if it has
394 if( strchr( "\"*:<>?|", c ) != NULL )
399 psz_name = p_sys->psz_file;
401 #if defined (WIN32) || defined (UNDER_CE)
406 if( asprintf( &p_sys->psz_file, "%s" DIR_SEP "%s",
407 p_sys->psz_path, psz_name ) == -1 )
408 p_sys->psz_file = NULL;
410 if( p_sys->psz_file == NULL )
412 p_sys->b_dump = false;
416 msg_Dbg( p_access, "dump in file '%s'", p_sys->psz_file );
418 p_sys->f = utf8_fopen( p_sys->psz_file, "wb" );
419 if( p_sys->f == NULL )
421 msg_Err( p_access, "cannot open file '%s' (%m)",
423 free( p_sys->psz_file );
424 p_sys->psz_file = NULL;
425 p_sys->b_dump = false;
429 Notify( p_access, true );
434 if( ( i_write = fwrite( p_buffer, 1, i_buffer, p_sys->f ) ) > 0 )
435 p_sys->i_size += i_write;