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 *****************************************************************************/
33 #include <vlc/input.h>
42 /*****************************************************************************
44 *****************************************************************************/
46 #define RECORD_PATH_TXT N_("Record directory")
47 #define RECORD_PATH_LONGTXT N_( \
48 "Directory where the record will be stored." )
50 static int Open ( vlc_object_t * );
51 static void Close( vlc_object_t * );
54 set_shortname( _("Record") );
55 set_description( _("Record") );
56 set_category( CAT_INPUT );
57 set_subcategory( SUBCAT_INPUT_ACCESS_FILTER );
58 set_capability( "access_filter", 0 );
59 add_shortcut( "record" );
61 add_directory( "record-path", NULL, NULL,
62 RECORD_PATH_TXT, RECORD_PATH_LONGTXT, VLC_TRUE );
64 set_callbacks( Open, Close );
68 /*****************************************************************************
70 *****************************************************************************/
72 static block_t *Block ( access_t * );
73 static int Read ( access_t *, uint8_t *, int );
74 static int Control( access_t *, int i_query, va_list args );
75 static int Seek ( access_t *, int64_t );
77 static void Dump( access_t *, uint8_t *, int );
79 static int EventKey( vlc_object_t *, char const *,
80 vlc_value_t, vlc_value_t, void * );
92 vout_thread_t *p_vout;
98 static inline void PreUpdateFlags( access_t *p_access )
100 access_t *p_src = p_access->p_source;
101 /* backport flags turned off 0 */
102 p_src->info.i_update &= p_access->p_sys->i_update_sav ^ (~p_access->info.i_update);
105 static inline void PostUpdateFlags( access_t *p_access )
107 access_t *p_src = p_access->p_source;
109 p_access->info = p_src->info;
110 p_access->p_sys->i_update_sav = p_access->info.i_update;
114 /*****************************************************************************
116 *****************************************************************************/
117 static int Open( vlc_object_t *p_this )
119 access_t *p_access = (access_t*)p_this;
120 access_t *p_src = p_access->p_source;
125 p_access->pf_read = p_src->pf_read ? Read : NULL;
126 p_access->pf_block = p_src->pf_block ? Block : NULL;
127 p_access->pf_seek = p_src->pf_seek ? Seek : NULL;
128 p_access->pf_control = Control;
131 p_access->info = p_src->info;
134 p_access->p_sys = p_sys = malloc( sizeof( access_t ) );
139 p_sys->psz_file = NULL;
140 p_sys->psz_ext = "dat";
141 p_sys->b_dump = VLC_FALSE;
142 p_sys->p_vout = NULL;
143 p_sys->i_vout_chan = -1;
144 p_sys->i_update_sav = p_access->info.i_update;
146 if( !strncasecmp( p_src->psz_access, "dvb", 3 ) ||
147 !strncasecmp( p_src->psz_access, "udp", 3 ) )
148 p_sys->psz_ext = "ts";
150 psz = var_CreateGetString( p_access, "record-path" );
154 if( p_access->p_vlc->psz_homedir )
155 psz = strdup( p_access->p_vlc->psz_homedir );
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_vlc, "key-pressed", 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_vlc, "key-pressed", 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;
195 PreUpdateFlags( p_access );
198 p_block = p_src->pf_block( p_src );
199 if( p_block && p_block->i_buffer )
200 Dump( p_access, p_block->p_buffer, p_block->i_buffer );
203 PostUpdateFlags( p_access );
208 /*****************************************************************************
210 *****************************************************************************/
211 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
213 access_t *p_src = p_access->p_source;
217 PreUpdateFlags( p_access );
220 i_ret = p_src->pf_read( p_src, p_buffer, i_len );
223 Dump( p_access, p_buffer, i_ret );
226 PostUpdateFlags( p_access );
231 /*****************************************************************************
233 *****************************************************************************/
234 static int Control( access_t *p_access, int i_query, va_list args )
236 access_t *p_src = p_access->p_source;
240 PreUpdateFlags( p_access );
243 i_ret = p_src->pf_control( p_src, i_query, args );
246 PostUpdateFlags( p_access );
251 /*****************************************************************************
253 *****************************************************************************/
254 static int Seek( access_t *p_access, int64_t i_pos )
256 access_t *p_src = p_access->p_source;
260 PreUpdateFlags( p_access );
263 i_ret = p_src->pf_seek( p_src, i_pos );
266 PostUpdateFlags( p_access );
271 /*****************************************************************************
273 *****************************************************************************/
274 static int EventKey( vlc_object_t *p_this, char const *psz_var,
275 vlc_value_t oldval, vlc_value_t newval, void *p_data )
277 access_t *p_access = p_data;
278 access_sys_t *p_sys = p_access->p_sys;
280 struct hotkey *p_hotkeys = p_access->p_vlc->p_hotkeys;
281 int i_action = -1, i;
283 for( i = 0; p_hotkeys[i].psz_action != NULL; i++ )
285 if( p_hotkeys[i].i_key == newval.i_int )
287 i_action = p_hotkeys[i].i_action;
291 if( i_action == ACTIONID_RECORD )
294 p_sys->b_dump = VLC_FALSE;
296 p_sys->b_dump = VLC_TRUE;
302 /*****************************************************************************
304 *****************************************************************************/
305 static void Notify( access_t *p_access, vlc_bool_t b_dump )
307 access_sys_t *p_sys = p_access->p_sys;
308 vout_thread_t *p_vout;
310 p_vout = vlc_object_find( p_access, VLC_OBJECT_VOUT, FIND_ANYWHERE );
311 if( !p_vout ) return;
313 if( p_vout != p_sys->p_vout )
315 p_sys->p_vout = p_vout;
316 if( spu_Control( p_vout->p_spu, SPU_CHANNEL_REGISTER,
317 &p_sys->i_vout_chan ) )
318 p_sys->i_vout_chan = -1;
321 if( p_sys->i_vout_chan != -1 )
324 vout_OSDMessage( p_vout, p_sys->i_vout_chan, "Recording" );
326 vout_OSDMessage( p_vout, p_sys->i_vout_chan, "Recording done" );
328 vlc_object_release( p_vout );
331 /*****************************************************************************
333 *****************************************************************************/
334 static void Dump( access_t *p_access, uint8_t *p_buffer, int i_buffer )
336 access_sys_t *p_sys = p_access->p_sys;
344 msg_Dbg( p_access, "dumped "I64Fd" kb (%s)",
345 p_sys->i_size/1024, p_sys->psz_file );
347 Notify( p_access, VLC_FALSE );
352 free( p_sys->psz_file );
353 p_sys->psz_file = NULL;
363 input_thread_t *p_input;
364 char *psz_name = NULL, *psz;
365 time_t t = time(NULL);
368 #ifdef HAVE_LOCALTIME_R
369 if( !localtime_r( &t, &l ) ) memset( &l, 0, sizeof(l) );
373 struct tm *p_l = localtime( &t );
375 else memset( &l, 0, sizeof(l) );
379 p_input = vlc_object_find( p_access, VLC_OBJECT_INPUT, FIND_PARENT );
382 vlc_mutex_lock( &p_input->input.p_item->lock );
383 if( p_input->input.p_item->psz_name )
385 char *p = strrchr( p_input->input.p_item->psz_name, '/' );
387 p = strrchr( p_input->input.p_item->psz_name, '\\' );
390 psz_name = strdup( p_input->input.p_item->psz_name );
391 else if( p[1] != '\0' )
392 psz_name = strdup( &p[1] );
394 vlc_mutex_unlock( &p_input->input.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;