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 *****************************************************************************/
30 #include <vlc_input.h>
31 #include <vlc_access.h>
35 #include <vlc_charset.h>
39 /*****************************************************************************
41 *****************************************************************************/
43 #define RECORD_PATH_TXT N_("Record directory")
44 #define RECORD_PATH_LONGTXT N_( \
45 "Directory where the record will be stored." )
47 static int Open ( vlc_object_t * );
48 static void Close( vlc_object_t * );
51 set_shortname( _("Record") );
52 set_description( _("Record") );
53 set_category( CAT_INPUT );
54 set_subcategory( SUBCAT_INPUT_ACCESS_FILTER );
55 set_capability( "access_filter", 0 );
56 add_shortcut( "record" );
58 add_directory( "record-path", NULL, NULL,
59 RECORD_PATH_TXT, RECORD_PATH_LONGTXT, VLC_TRUE );
62 set_callbacks( Open, Close );
66 /*****************************************************************************
68 *****************************************************************************/
70 static block_t *Block ( access_t * );
71 static ssize_t Read ( access_t *, uint8_t *, size_t );
72 static int Control( access_t *, int i_query, va_list args );
73 static int Seek ( access_t *, int64_t );
75 static void Dump( access_t *, uint8_t *, int );
77 static int EventKey( vlc_object_t *, char const *,
78 vlc_value_t, vlc_value_t, void * );
90 vout_thread_t *p_vout;
96 static inline void PreUpdateFlags( access_t *p_access )
98 access_t *p_src = p_access->p_source;
99 /* backport flags turned off 0 */
100 p_src->info.i_update &= p_access->p_sys->i_update_sav ^ (~p_access->info.i_update);
103 static inline void PostUpdateFlags( access_t *p_access )
105 access_t *p_src = p_access->p_source;
107 p_access->info = p_src->info;
108 p_access->p_sys->i_update_sav = p_access->info.i_update;
112 /*****************************************************************************
114 *****************************************************************************/
115 static int Open( vlc_object_t *p_this )
117 access_t *p_access = (access_t*)p_this;
118 access_t *p_src = p_access->p_source;
123 p_access->pf_read = p_src->pf_read ? Read : NULL;
124 p_access->pf_block = p_src->pf_block ? Block : NULL;
125 p_access->pf_seek = p_src->pf_seek ? Seek : NULL;
126 p_access->pf_control = Control;
129 p_access->info = p_src->info;
132 p_access->p_sys = p_sys = malloc( sizeof( access_t ) );
137 p_sys->psz_file = NULL;
138 p_sys->psz_ext = "dat";
139 p_sys->b_dump = VLC_FALSE;
140 p_sys->p_vout = NULL;
141 p_sys->i_vout_chan = -1;
142 p_sys->i_update_sav = p_access->info.i_update;
144 if( !strncasecmp( p_src->psz_access, "dvb", 3 ) ||
145 !strncasecmp( p_src->psz_access, "udp", 3 ) )
146 p_sys->psz_ext = "ts";
148 psz = var_CreateGetString( p_access, "record-path" );
152 if( p_access->p_libvlc->psz_homedir ) /* XXX: This should never happen */
153 psz = strdup( p_access->p_libvlc->psz_homedir );
155 p_sys->psz_path = psz;
156 msg_Dbg( p_access, "Record access filter path %s", psz );
158 /* catch all key event */
159 var_AddCallback( p_access->p_libvlc, "key-pressed", EventKey, p_access );
164 /*****************************************************************************
166 *****************************************************************************/
167 static void Close( vlc_object_t *p_this )
169 access_t *p_access = (access_t*)p_this;
170 access_sys_t *p_sys = p_access->p_sys;
172 var_DelCallback( p_access->p_libvlc, "key-pressed", EventKey, p_access );
177 free( p_sys->psz_file );
180 free( p_sys->psz_path );
184 /*****************************************************************************
186 *****************************************************************************/
187 static block_t *Block( access_t *p_access )
189 access_t *p_src = p_access->p_source;
193 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 );
201 PostUpdateFlags( p_access );
206 /*****************************************************************************
208 *****************************************************************************/
209 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
211 access_t *p_src = p_access->p_source;
215 PreUpdateFlags( p_access );
218 i_ret = p_src->pf_read( p_src, p_buffer, i_len );
221 Dump( p_access, p_buffer, i_ret );
224 PostUpdateFlags( p_access );
229 /*****************************************************************************
231 *****************************************************************************/
232 static int Control( access_t *p_access, int i_query, va_list args )
234 access_t *p_src = p_access->p_source;
238 PreUpdateFlags( p_access );
241 i_ret = p_src->pf_control( p_src, i_query, args );
244 PostUpdateFlags( p_access );
249 /*****************************************************************************
251 *****************************************************************************/
252 static int Seek( access_t *p_access, int64_t i_pos )
254 access_t *p_src = p_access->p_source;
258 PreUpdateFlags( p_access );
261 i_ret = p_src->pf_seek( p_src, i_pos );
264 PostUpdateFlags( p_access );
269 /*****************************************************************************
271 *****************************************************************************/
272 static int EventKey( vlc_object_t *p_this, char const *psz_var,
273 vlc_value_t oldval, vlc_value_t newval, void *p_data )
275 access_t *p_access = p_data;
276 access_sys_t *p_sys = p_access->p_sys;
278 struct hotkey *p_hotkeys = p_access->p_libvlc->p_hotkeys;
279 int i_action = -1, i;
281 for( i = 0; p_hotkeys[i].psz_action != NULL; i++ )
283 if( p_hotkeys[i].i_key == newval.i_int )
285 i_action = p_hotkeys[i].i_action;
289 if( i_action == ACTIONID_RECORD )
292 p_sys->b_dump = VLC_FALSE;
294 p_sys->b_dump = VLC_TRUE;
300 /*****************************************************************************
302 *****************************************************************************/
303 static void Notify( access_t *p_access, vlc_bool_t b_dump )
305 access_sys_t *p_sys = p_access->p_sys;
306 vout_thread_t *p_vout;
308 p_vout = vlc_object_find( p_access, VLC_OBJECT_VOUT, FIND_ANYWHERE );
309 if( !p_vout ) return;
311 if( p_vout != p_sys->p_vout )
313 p_sys->p_vout = p_vout;
314 if( spu_Control( p_vout->p_spu, SPU_CHANNEL_REGISTER,
315 &p_sys->i_vout_chan ) )
316 p_sys->i_vout_chan = -1;
319 if( p_sys->i_vout_chan != -1 )
322 vout_OSDMessage( p_vout, p_sys->i_vout_chan, _("Recording") );
324 vout_OSDMessage( p_vout, p_sys->i_vout_chan, _("Recording done") );
326 vlc_object_release( p_vout );
329 /*****************************************************************************
331 *****************************************************************************/
332 static void Dump( access_t *p_access, uint8_t *p_buffer, int i_buffer )
334 access_sys_t *p_sys = p_access->p_sys;
342 msg_Dbg( p_access, "dumped "I64Fd" kb (%s)",
343 p_sys->i_size/1024, p_sys->psz_file );
345 Notify( p_access, VLC_FALSE );
350 free( p_sys->psz_file );
351 p_sys->psz_file = NULL;
361 input_thread_t *p_input;
362 char *psz_name = NULL, *psz;
363 time_t t = time(NULL);
366 #ifdef HAVE_LOCALTIME_R
367 if( !localtime_r( &t, &l ) ) memset( &l, 0, sizeof(l) );
371 struct tm *p_l = localtime( &t );
373 else memset( &l, 0, sizeof(l) );
377 p_input = vlc_object_find( p_access, VLC_OBJECT_INPUT, FIND_PARENT );
380 input_item_t * p_item = input_GetItem( p_input );
381 vlc_mutex_lock( &p_item->lock );
382 if( p_item->psz_name )
384 char *p = strrchr( p_item->psz_name, '/' );
386 p = strrchr( p_item->psz_name, '\\' );
389 psz_name = strdup( p_item->psz_name );
390 else if( p[1] != '\0' )
391 psz_name = strdup( &p[1] );
393 vlc_mutex_unlock( &p_item->lock );
395 vlc_object_release( p_input );
398 if( asprintf( &p_sys->psz_file, "%s %d-%d-%d %.2dh%.2dm%.2ds.%s",
399 ( psz_name != NULL ) ? psz_name : "Unknown",
400 l.tm_mday, l.tm_mon+1, l.tm_year+1900,
401 l.tm_hour, l.tm_min, l.tm_sec,
402 p_sys->psz_ext ) == -1 )
403 p_sys->psz_file = NULL;
406 if( p_sys->psz_file == NULL )
408 p_sys->b_dump = VLC_FALSE;
412 /* Remove all forbidden characters (except (back)slashes) */
413 for( psz = p_sys->psz_file; *psz; psz++ )
415 unsigned char c = (unsigned char)*psz;
417 /* Even if many OS accept non printable characters, we remove
418 * them to avoid confusing users */
419 if( ( c < 32 ) || ( c == 127 ) )
421 #if defined (WIN32) || defined (UNDER_CE)
422 /* Windows has a lot of forbidden characters, even if it has
424 if( strchr( "\"*:<>?|", c ) != NULL )
429 psz_name = p_sys->psz_file;
431 #if defined (WIN32) || defined (UNDER_CE)
436 if( asprintf( &p_sys->psz_file, "%s" DIR_SEP "%s",
437 p_sys->psz_path, psz_name ) == -1 )
438 p_sys->psz_file = NULL;
440 if( p_sys->psz_file == NULL )
442 p_sys->b_dump = VLC_FALSE;
446 msg_Dbg( p_access, "dump in file '%s'", p_sys->psz_file );
448 p_sys->f = utf8_fopen( p_sys->psz_file, "wb" );
449 if( p_sys->f == NULL )
451 msg_Err( p_access, "cannot open file '%s' (%m)",
453 free( p_sys->psz_file );
454 p_sys->psz_file = NULL;
455 p_sys->b_dump = VLC_FALSE;
459 Notify( p_access, VLC_TRUE );
465 if( ( i_write = fwrite( p_buffer, 1, i_buffer, p_sys->f ) ) > 0 )
466 p_sys->i_size += i_write;