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_input.h>
33 #include <vlc_access.h>
37 #include <vlc_charset.h>
41 /*****************************************************************************
43 *****************************************************************************/
45 #define RECORD_PATH_TXT N_("Record directory")
46 #define RECORD_PATH_LONGTXT N_( \
47 "Directory where the record will be stored." )
49 static int Open ( vlc_object_t * );
50 static void Close( vlc_object_t * );
53 set_shortname( _("Record") );
54 set_description( _("Record") );
55 set_category( CAT_INPUT );
56 set_subcategory( SUBCAT_INPUT_ACCESS_FILTER );
57 set_capability( "access_filter", 0 );
58 add_shortcut( "record" );
60 add_directory( "record-path", NULL, NULL,
61 RECORD_PATH_TXT, RECORD_PATH_LONGTXT, VLC_TRUE );
63 set_callbacks( Open, Close );
67 /*****************************************************************************
69 *****************************************************************************/
71 static block_t *Block ( access_t * );
72 static int Read ( access_t *, uint8_t *, int );
73 static int Control( access_t *, int i_query, va_list args );
74 static int Seek ( access_t *, int64_t );
76 static void Dump( access_t *, uint8_t *, int );
78 static int EventKey( vlc_object_t *, char const *,
79 vlc_value_t, vlc_value_t, void * );
91 vout_thread_t *p_vout;
97 static inline void PreUpdateFlags( access_t *p_access )
99 access_t *p_src = p_access->p_source;
100 /* backport flags turned off 0 */
101 p_src->info.i_update &= p_access->p_sys->i_update_sav ^ (~p_access->info.i_update);
104 static inline void PostUpdateFlags( access_t *p_access )
106 access_t *p_src = p_access->p_source;
108 p_access->info = p_src->info;
109 p_access->p_sys->i_update_sav = p_access->info.i_update;
113 /*****************************************************************************
115 *****************************************************************************/
116 static int Open( vlc_object_t *p_this )
118 access_t *p_access = (access_t*)p_this;
119 access_t *p_src = p_access->p_source;
124 p_access->pf_read = p_src->pf_read ? Read : NULL;
125 p_access->pf_block = p_src->pf_block ? Block : NULL;
126 p_access->pf_seek = p_src->pf_seek ? Seek : NULL;
127 p_access->pf_control = Control;
130 p_access->info = p_src->info;
133 p_access->p_sys = p_sys = malloc( sizeof( access_t ) );
138 p_sys->psz_file = NULL;
139 p_sys->psz_ext = "dat";
140 p_sys->b_dump = VLC_FALSE;
141 p_sys->p_vout = NULL;
142 p_sys->i_vout_chan = -1;
143 p_sys->i_update_sav = p_access->info.i_update;
145 if( !strncasecmp( p_src->psz_access, "dvb", 3 ) ||
146 !strncasecmp( p_src->psz_access, "udp", 3 ) )
147 p_sys->psz_ext = "ts";
149 psz = var_CreateGetString( p_access, "record-path" );
153 if( p_access->p_libvlc->psz_homedir )
154 psz = strdup( p_access->p_libvlc->psz_homedir );
156 p_sys->psz_path = psz;
157 msg_Dbg( p_access, "Record access filter path %s", psz );
159 /* catch all key event */
160 var_AddCallback( p_access->p_libvlc, "key-pressed", EventKey, p_access );
165 /*****************************************************************************
167 *****************************************************************************/
168 static void Close( vlc_object_t *p_this )
170 access_t *p_access = (access_t*)p_this;
171 access_sys_t *p_sys = p_access->p_sys;
173 var_DelCallback( p_access->p_libvlc, "key-pressed", EventKey, p_access );
178 free( p_sys->psz_file );
181 free( p_sys->psz_path );
185 /*****************************************************************************
187 *****************************************************************************/
188 static block_t *Block( access_t *p_access )
190 access_t *p_src = p_access->p_source;
194 PreUpdateFlags( p_access );
197 p_block = p_src->pf_block( p_src );
198 if( p_block && p_block->i_buffer )
199 Dump( p_access, p_block->p_buffer, p_block->i_buffer );
202 PostUpdateFlags( p_access );
207 /*****************************************************************************
209 *****************************************************************************/
210 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
212 access_t *p_src = p_access->p_source;
216 PreUpdateFlags( p_access );
219 i_ret = p_src->pf_read( p_src, p_buffer, i_len );
222 Dump( p_access, p_buffer, i_ret );
225 PostUpdateFlags( p_access );
230 /*****************************************************************************
232 *****************************************************************************/
233 static int Control( access_t *p_access, int i_query, va_list args )
235 access_t *p_src = p_access->p_source;
239 PreUpdateFlags( p_access );
242 i_ret = p_src->pf_control( p_src, i_query, args );
245 PostUpdateFlags( p_access );
250 /*****************************************************************************
252 *****************************************************************************/
253 static int Seek( access_t *p_access, int64_t i_pos )
255 access_t *p_src = p_access->p_source;
259 PreUpdateFlags( p_access );
262 i_ret = p_src->pf_seek( p_src, i_pos );
265 PostUpdateFlags( p_access );
270 /*****************************************************************************
272 *****************************************************************************/
273 static int EventKey( vlc_object_t *p_this, char const *psz_var,
274 vlc_value_t oldval, vlc_value_t newval, void *p_data )
276 access_t *p_access = p_data;
277 access_sys_t *p_sys = p_access->p_sys;
279 struct hotkey *p_hotkeys = p_access->p_libvlc->p_hotkeys;
280 int i_action = -1, i;
282 for( i = 0; p_hotkeys[i].psz_action != NULL; i++ )
284 if( p_hotkeys[i].i_key == newval.i_int )
286 i_action = p_hotkeys[i].i_action;
290 if( i_action == ACTIONID_RECORD )
293 p_sys->b_dump = VLC_FALSE;
295 p_sys->b_dump = VLC_TRUE;
301 /*****************************************************************************
303 *****************************************************************************/
304 static void Notify( access_t *p_access, vlc_bool_t b_dump )
306 access_sys_t *p_sys = p_access->p_sys;
307 vout_thread_t *p_vout;
309 p_vout = vlc_object_find( p_access, VLC_OBJECT_VOUT, FIND_ANYWHERE );
310 if( !p_vout ) return;
312 if( p_vout != p_sys->p_vout )
314 p_sys->p_vout = p_vout;
315 if( spu_Control( p_vout->p_spu, SPU_CHANNEL_REGISTER,
316 &p_sys->i_vout_chan ) )
317 p_sys->i_vout_chan = -1;
320 if( p_sys->i_vout_chan != -1 )
323 vout_OSDMessage( p_vout, p_sys->i_vout_chan, _("Recording") );
325 vout_OSDMessage( p_vout, p_sys->i_vout_chan, _("Recording done") );
327 vlc_object_release( p_vout );
330 /*****************************************************************************
332 *****************************************************************************/
333 static void Dump( access_t *p_access, uint8_t *p_buffer, int i_buffer )
335 access_sys_t *p_sys = p_access->p_sys;
343 msg_Dbg( p_access, "dumped "I64Fd" kb (%s)",
344 p_sys->i_size/1024, p_sys->psz_file );
346 Notify( p_access, VLC_FALSE );
351 free( p_sys->psz_file );
352 p_sys->psz_file = NULL;
362 input_thread_t *p_input;
363 char *psz_name = NULL, *psz;
364 time_t t = time(NULL);
367 #ifdef HAVE_LOCALTIME_R
368 if( !localtime_r( &t, &l ) ) memset( &l, 0, sizeof(l) );
372 struct tm *p_l = localtime( &t );
374 else memset( &l, 0, sizeof(l) );
378 p_input = vlc_object_find( p_access, VLC_OBJECT_INPUT, FIND_PARENT );
381 input_item_t * p_item = input_GetItem( p_input );
382 vlc_mutex_lock( &p_item->lock );
383 if( p_item->psz_name )
385 char *p = strrchr( p_item->psz_name, '/' );
387 p = strrchr( p_item->psz_name, '\\' );
390 psz_name = strdup( p_item->psz_name );
391 else if( p[1] != '\0' )
392 psz_name = strdup( &p[1] );
394 vlc_mutex_unlock( &p_item->lock );
396 vlc_object_release( p_input );
399 if( psz_name == NULL )
400 psz_name = strdup( "Unknown" );
402 asprintf( &p_sys->psz_file, "%s %d-%d-%d %.2dh%.2dm%.2ds.%s",
404 l.tm_mday, l.tm_mon+1, l.tm_year+1900,
405 l.tm_hour, l.tm_min, l.tm_sec,
410 /* Remove all forbidden characters (except (back)slashes) */
411 for( psz = p_sys->psz_file; *psz; psz++ )
413 unsigned char c = (unsigned char)*psz;
415 /* Even if many OS accept non printable characters, we remove
416 * them to avoid confusing users */
417 if( ( c < 32 ) || ( c == 127 ) )
419 #if defined (WIN32) || defined (UNDER_CE)
420 /* Windows has a lot of forbidden characters, even if it has
422 if( strchr( "\"*:<>?|", c ) != NULL )
427 psz_name=strdup(p_sys->psz_file);
429 #if defined (WIN32) || defined (UNDER_CE)
434 asprintf(&p_sys->psz_file, "%s" DIR_SEP "%s",
435 p_sys->psz_path, psz_name);
438 msg_Dbg( p_access, "dump in file '%s'", p_sys->psz_file );
440 p_sys->f = utf8_fopen( p_sys->psz_file, "wb" );
441 if( p_sys->f == NULL )
443 msg_Err( p_access, "cannot open file '%s' (%s)",
444 p_sys->psz_file, strerror(errno) );
445 free( p_sys->psz_file );
446 p_sys->psz_file = NULL;
447 p_sys->b_dump = VLC_FALSE;
451 Notify( p_access, VLC_TRUE );
457 if( ( i_write = fwrite( p_buffer, 1, i_buffer, p_sys->f ) ) > 0 )
458 p_sys->i_size += i_write;