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 *****************************************************************************/
34 #include <vlc_input.h>
35 #include <vlc_access.h>
39 #include <vlc_charset.h>
43 /*****************************************************************************
45 *****************************************************************************/
47 #define RECORD_PATH_TXT N_("Record directory")
48 #define RECORD_PATH_LONGTXT N_( \
49 "Directory where the record will be stored." )
51 static int Open ( vlc_object_t * );
52 static void Close( vlc_object_t * );
55 set_shortname( _("Record") );
56 set_description( _("Record") );
57 set_category( CAT_INPUT );
58 set_subcategory( SUBCAT_INPUT_ACCESS_FILTER );
59 set_capability( "access_filter", 0 );
60 add_shortcut( "record" );
62 add_directory( "record-path", NULL, NULL,
63 RECORD_PATH_TXT, RECORD_PATH_LONGTXT, VLC_TRUE );
66 set_callbacks( Open, Close );
70 /*****************************************************************************
72 *****************************************************************************/
74 static block_t *Block ( access_t * );
75 static ssize_t Read ( access_t *, uint8_t *, size_t );
76 static int Control( access_t *, int i_query, va_list args );
77 static int Seek ( access_t *, int64_t );
79 static void Dump( access_t *, uint8_t *, int );
81 static int EventKey( vlc_object_t *, char const *,
82 vlc_value_t, vlc_value_t, void * );
94 vout_thread_t *p_vout;
100 static inline void PreUpdateFlags( access_t *p_access )
102 access_t *p_src = p_access->p_source;
103 /* backport flags turned off 0 */
104 p_src->info.i_update &= p_access->p_sys->i_update_sav ^ (~p_access->info.i_update);
107 static inline void PostUpdateFlags( access_t *p_access )
109 access_t *p_src = p_access->p_source;
111 p_access->info = p_src->info;
112 p_access->p_sys->i_update_sav = p_access->info.i_update;
116 /*****************************************************************************
118 *****************************************************************************/
119 static int Open( vlc_object_t *p_this )
121 access_t *p_access = (access_t*)p_this;
122 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;
133 p_access->info = p_src->info;
136 p_access->p_sys = p_sys = malloc( sizeof( access_t ) );
141 p_sys->psz_file = NULL;
142 p_sys->psz_ext = "dat";
143 p_sys->b_dump = VLC_FALSE;
144 p_sys->p_vout = NULL;
145 p_sys->i_vout_chan = -1;
146 p_sys->i_update_sav = p_access->info.i_update;
148 if( !strncasecmp( p_src->psz_access, "dvb", 3 ) ||
149 !strncasecmp( p_src->psz_access, "udp", 3 ) )
150 p_sys->psz_ext = "ts";
152 psz = var_CreateGetString( p_access, "record-path" );
156 if( p_access->p_libvlc->psz_homedir ) /* XXX: This should never happen */
157 psz = strdup( p_access->p_libvlc->psz_homedir );
159 p_sys->psz_path = psz;
160 msg_Dbg( p_access, "Record access filter path %s", psz );
162 /* catch all key event */
163 var_AddCallback( p_access->p_libvlc, "key-pressed", EventKey, p_access );
168 /*****************************************************************************
170 *****************************************************************************/
171 static void Close( vlc_object_t *p_this )
173 access_t *p_access = (access_t*)p_this;
174 access_sys_t *p_sys = p_access->p_sys;
176 var_DelCallback( p_access->p_libvlc, "key-pressed", EventKey, p_access );
181 free( p_sys->psz_file );
184 free( p_sys->psz_path );
188 /*****************************************************************************
190 *****************************************************************************/
191 static block_t *Block( access_t *p_access )
193 access_t *p_src = p_access->p_source;
197 PreUpdateFlags( p_access );
200 p_block = p_src->pf_block( p_src );
201 if( p_block && p_block->i_buffer )
202 Dump( p_access, p_block->p_buffer, p_block->i_buffer );
205 PostUpdateFlags( p_access );
210 /*****************************************************************************
212 *****************************************************************************/
213 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
215 access_t *p_src = p_access->p_source;
219 PreUpdateFlags( p_access );
222 i_ret = p_src->pf_read( p_src, p_buffer, i_len );
225 Dump( p_access, p_buffer, i_ret );
228 PostUpdateFlags( p_access );
233 /*****************************************************************************
235 *****************************************************************************/
236 static int Control( access_t *p_access, int i_query, va_list args )
238 access_t *p_src = p_access->p_source;
242 PreUpdateFlags( p_access );
245 i_ret = p_src->pf_control( p_src, i_query, args );
248 PostUpdateFlags( p_access );
253 /*****************************************************************************
255 *****************************************************************************/
256 static int Seek( access_t *p_access, int64_t i_pos )
258 access_t *p_src = p_access->p_source;
262 PreUpdateFlags( p_access );
265 i_ret = p_src->pf_seek( p_src, i_pos );
268 PostUpdateFlags( p_access );
273 /*****************************************************************************
275 *****************************************************************************/
276 static int EventKey( vlc_object_t *p_this, char const *psz_var,
277 vlc_value_t oldval, vlc_value_t newval, void *p_data )
279 access_t *p_access = p_data;
280 access_sys_t *p_sys = p_access->p_sys;
282 struct hotkey *p_hotkeys = p_access->p_libvlc->p_hotkeys;
283 int i_action = -1, i;
285 for( i = 0; p_hotkeys[i].psz_action != NULL; i++ )
287 if( p_hotkeys[i].i_key == newval.i_int )
289 i_action = p_hotkeys[i].i_action;
293 if( i_action == ACTIONID_RECORD )
296 p_sys->b_dump = VLC_FALSE;
298 p_sys->b_dump = VLC_TRUE;
304 /*****************************************************************************
306 *****************************************************************************/
307 static void Notify( access_t *p_access, vlc_bool_t b_dump )
309 access_sys_t *p_sys = p_access->p_sys;
310 vout_thread_t *p_vout;
312 p_vout = vlc_object_find( p_access, VLC_OBJECT_VOUT, FIND_ANYWHERE );
313 if( !p_vout ) return;
315 if( p_vout != p_sys->p_vout )
317 p_sys->p_vout = p_vout;
318 if( spu_Control( p_vout->p_spu, SPU_CHANNEL_REGISTER,
319 &p_sys->i_vout_chan ) )
320 p_sys->i_vout_chan = -1;
323 if( p_sys->i_vout_chan != -1 )
326 vout_OSDMessage( p_vout, p_sys->i_vout_chan, _("Recording") );
328 vout_OSDMessage( p_vout, p_sys->i_vout_chan, _("Recording done") );
330 vlc_object_release( p_vout );
333 /*****************************************************************************
335 *****************************************************************************/
336 static void Dump( access_t *p_access, uint8_t *p_buffer, int i_buffer )
338 access_sys_t *p_sys = p_access->p_sys;
346 msg_Dbg( p_access, "dumped "I64Fd" kb (%s)",
347 p_sys->i_size/1024, p_sys->psz_file );
349 Notify( p_access, VLC_FALSE );
354 free( p_sys->psz_file );
355 p_sys->psz_file = NULL;
365 input_thread_t *p_input;
366 char *psz_name = NULL, *psz;
367 time_t t = time(NULL);
370 if( !localtime_r( &t, &l ) ) memset( &l, 0, sizeof(l) );
372 p_input = vlc_object_find( p_access, VLC_OBJECT_INPUT, FIND_PARENT );
375 input_item_t * p_item = input_GetItem( p_input );
376 vlc_mutex_lock( &p_item->lock );
377 if( p_item->psz_name )
379 char *p = strrchr( p_item->psz_name, '/' );
381 p = strrchr( p_item->psz_name, '\\' );
384 psz_name = strdup( p_item->psz_name );
385 else if( p[1] != '\0' )
386 psz_name = strdup( &p[1] );
388 vlc_mutex_unlock( &p_item->lock );
390 vlc_object_release( p_input );
393 if( asprintf( &p_sys->psz_file, "%s %d-%d-%d %.2dh%.2dm%.2ds.%s",
394 ( psz_name != NULL ) ? psz_name : "Unknown",
395 l.tm_mday, l.tm_mon+1, l.tm_year+1900,
396 l.tm_hour, l.tm_min, l.tm_sec,
397 p_sys->psz_ext ) == -1 )
398 p_sys->psz_file = NULL;
401 if( p_sys->psz_file == NULL )
403 p_sys->b_dump = VLC_FALSE;
407 /* Remove all forbidden characters (except (back)slashes) */
408 for( psz = p_sys->psz_file; *psz; psz++ )
410 unsigned char c = (unsigned char)*psz;
412 /* Even if many OS accept non printable characters, we remove
413 * them to avoid confusing users */
414 if( ( c < 32 ) || ( c == 127 ) )
416 #if defined (WIN32) || defined (UNDER_CE)
417 /* Windows has a lot of forbidden characters, even if it has
419 if( strchr( "\"*:<>?|", c ) != NULL )
424 psz_name = p_sys->psz_file;
426 #if defined (WIN32) || defined (UNDER_CE)
431 if( asprintf( &p_sys->psz_file, "%s" DIR_SEP "%s",
432 p_sys->psz_path, psz_name ) == -1 )
433 p_sys->psz_file = NULL;
435 if( p_sys->psz_file == NULL )
437 p_sys->b_dump = VLC_FALSE;
441 msg_Dbg( p_access, "dump in file '%s'", p_sys->psz_file );
443 p_sys->f = utf8_fopen( p_sys->psz_file, "wb" );
444 if( p_sys->f == NULL )
446 msg_Err( p_access, "cannot open file '%s' (%m)",
448 free( p_sys->psz_file );
449 p_sys->psz_file = NULL;
450 p_sys->b_dump = VLC_FALSE;
454 Notify( p_access, VLC_TRUE );
460 if( ( i_write = fwrite( p_buffer, 1, i_buffer, p_sys->f ) ) > 0 )
461 p_sys->i_size += i_write;