1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2005 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>
38 /*****************************************************************************
40 *****************************************************************************/
42 #define RECORD_PATH_TXT N_("Record directory")
43 #define RECORD_PATH_LONGTXT N_( \
44 "Allows you to specify the directory where the record will be stored" )
46 static int Open ( vlc_object_t * );
47 static void Close( vlc_object_t * );
50 set_shortname( _("Record") );
51 set_description( _("Record") );
52 set_category( CAT_INPUT );
53 set_subcategory( SUBCAT_INPUT_ACCESS_FILTER );
54 set_capability( "access_filter", 0 );
55 add_shortcut( "record" );
57 add_directory( "record-path", NULL, NULL,
58 RECORD_PATH_TXT, RECORD_PATH_LONGTXT, VLC_TRUE );
60 set_callbacks( Open, Close );
64 /*****************************************************************************
66 *****************************************************************************/
68 static block_t *Block ( access_t * );
69 static int Read ( access_t *, uint8_t *, int );
70 static int Control( access_t *, int i_query, va_list args );
71 static int Seek ( access_t *, int64_t );
73 static void Dump( access_t *, uint8_t *, int );
75 static int EventKey( vlc_object_t *, char const *,
76 vlc_value_t, vlc_value_t, void * );
88 vout_thread_t *p_vout;
94 static inline void PreUpdateFlags( access_t *p_access )
96 access_t *p_src = p_access->p_source;
97 /* backport flags turned off 0 */
98 p_src->info.i_update &= p_access->p_sys->i_update_sav ^ (~p_access->info.i_update);
101 static inline void PostUpdateFlags( access_t *p_access )
103 access_t *p_src = p_access->p_source;
105 p_access->info = p_src->info;
106 p_access->p_sys->i_update_sav = p_access->info.i_update;
110 /*****************************************************************************
112 *****************************************************************************/
113 static int Open( vlc_object_t *p_this )
115 access_t *p_access = (access_t*)p_this;
116 access_t *p_src = p_access->p_source;
121 p_access->pf_read = p_src->pf_read ? Read : NULL;
122 p_access->pf_block = p_src->pf_block ? Block : NULL;
123 p_access->pf_seek = p_src->pf_seek ? Seek : NULL;
124 p_access->pf_control = Control;
127 p_access->info = p_src->info;
130 p_access->p_sys = p_sys = malloc( sizeof( access_t ) );
135 p_sys->psz_file = NULL;
136 p_sys->psz_ext = "dat";
137 p_sys->b_dump = VLC_FALSE;
138 p_sys->p_vout = NULL;
139 p_sys->i_vout_chan = -1;
140 p_sys->i_update_sav = p_access->info.i_update;
142 if( !strncasecmp( p_src->psz_access, "dvb", 3 ) ||
143 !strncasecmp( p_src->psz_access, "udp", 3 ) )
144 p_sys->psz_ext = "ts";
146 psz = var_CreateGetString( p_access, "record-path" );
150 if( p_access->p_vlc->psz_homedir )
151 psz = strdup( p_access->p_vlc->psz_homedir );
153 p_sys->psz_path = psz;
154 msg_Dbg( p_access, "Record access filter path %s", psz );
156 /* catch all key event */
157 var_AddCallback( p_access->p_vlc, "key-pressed", EventKey, p_access );
162 /*****************************************************************************
164 *****************************************************************************/
165 static void Close( vlc_object_t *p_this )
167 access_t *p_access = (access_t*)p_this;
168 access_sys_t *p_sys = p_access->p_sys;
170 var_DelCallback( p_access->p_vlc, "key-pressed", EventKey, p_access );
175 free( p_sys->psz_file );
178 free( p_sys->psz_path );
182 /*****************************************************************************
184 *****************************************************************************/
185 static block_t *Block( access_t *p_access )
187 access_t *p_src = p_access->p_source;
191 PreUpdateFlags( p_access );
194 p_block = p_src->pf_block( p_src );
195 if( p_block && p_block->i_buffer )
196 Dump( p_access, p_block->p_buffer, p_block->i_buffer );
199 PostUpdateFlags( p_access );
204 /*****************************************************************************
206 *****************************************************************************/
207 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
209 access_t *p_src = p_access->p_source;
213 PreUpdateFlags( p_access );
216 i_ret = p_src->pf_read( p_src, p_buffer, i_len );
219 Dump( p_access, p_buffer, i_ret );
222 PostUpdateFlags( p_access );
227 /*****************************************************************************
229 *****************************************************************************/
230 static int Control( access_t *p_access, int i_query, va_list args )
232 access_t *p_src = p_access->p_source;
236 PreUpdateFlags( p_access );
239 i_ret = p_src->pf_control( p_src, i_query, args );
242 PostUpdateFlags( p_access );
247 /*****************************************************************************
249 *****************************************************************************/
250 static int Seek( access_t *p_access, int64_t i_pos )
252 access_t *p_src = p_access->p_source;
256 PreUpdateFlags( p_access );
259 i_ret = p_src->pf_seek( p_src, i_pos );
262 PostUpdateFlags( p_access );
267 /*****************************************************************************
269 *****************************************************************************/
270 static int EventKey( vlc_object_t *p_this, char const *psz_var,
271 vlc_value_t oldval, vlc_value_t newval, void *p_data )
273 access_t *p_access = p_data;
274 access_sys_t *p_sys = p_access->p_sys;
276 struct hotkey *p_hotkeys = p_access->p_vlc->p_hotkeys;
277 int i_action = -1, i;
279 for( i = 0; p_hotkeys[i].psz_action != NULL; i++ )
281 if( p_hotkeys[i].i_key == newval.i_int )
283 i_action = p_hotkeys[i].i_action;
287 if( i_action == ACTIONID_RECORD )
290 p_sys->b_dump = VLC_FALSE;
292 p_sys->b_dump = VLC_TRUE;
298 /*****************************************************************************
300 *****************************************************************************/
301 static void Notify( access_t *p_access, vlc_bool_t b_dump )
303 access_sys_t *p_sys = p_access->p_sys;
304 vout_thread_t *p_vout;
306 p_vout = vlc_object_find( p_access, VLC_OBJECT_VOUT, FIND_ANYWHERE );
307 if( !p_vout ) return;
309 if( p_vout != p_sys->p_vout )
311 p_sys->p_vout = p_vout;
312 if( spu_Control( p_vout->p_spu, SPU_CHANNEL_REGISTER,
313 &p_sys->i_vout_chan ) )
314 p_sys->i_vout_chan = -1;
317 if( p_sys->i_vout_chan != -1 )
320 vout_OSDMessage( p_vout, p_sys->i_vout_chan, "Recording" );
322 vout_OSDMessage( p_vout, p_sys->i_vout_chan, "Recording done" );
324 vlc_object_release( p_vout );
327 /*****************************************************************************
329 *****************************************************************************/
330 static void Dump( access_t *p_access, uint8_t *p_buffer, int i_buffer )
332 access_sys_t *p_sys = p_access->p_sys;
340 msg_Dbg( p_access, "dumped "I64Fd" kb (%s)",
341 p_sys->i_size/1024, p_sys->psz_file );
343 Notify( p_access, VLC_FALSE );
348 free( p_sys->psz_file );
349 p_sys->psz_file = NULL;
359 input_thread_t *p_input;
360 char *psz_name = NULL;
361 time_t t = time(NULL);
364 #ifdef HAVE_LOCALTIME_R
365 if( !localtime_r( &t, &l ) ) memset( &l, 0, sizeof(l) );
369 struct tm *p_l = localtime( &t );
371 else memset( &l, 0, sizeof(l) );
375 p_input = vlc_object_find( p_access, VLC_OBJECT_INPUT, FIND_PARENT );
378 vlc_mutex_lock( &p_input->input.p_item->lock );
379 if( p_input->input.p_item->psz_name )
381 char *p = strrchr( p_input->input.p_item->psz_name, '/' );
383 p = strrchr( p_input->input.p_item->psz_name, '\\' );
386 psz_name = strdup( p_input->input.p_item->psz_name );
387 else if( p[1] != '\0' )
388 psz_name = strdup( &p[1] );
390 vlc_mutex_unlock( &p_input->input.p_item->lock );
392 vlc_object_release( p_input );
395 if( psz_name == NULL )
396 psz_name = strdup( "Unknown" );
398 asprintf( &p_sys->psz_file, "%s/%s %d-%d-%d %.2dh%.2dm%.2ds.%s",
399 p_sys->psz_path, psz_name,
400 l.tm_mday, l.tm_mon+1, l.tm_year+1900,
401 l.tm_hour, l.tm_min, l.tm_sec,
406 msg_Dbg( p_access, "dump in file '%s'", p_sys->psz_file );
408 p_sys->f = fopen( p_sys->psz_file, "wb" );
409 if( p_sys->f == NULL )
411 msg_Err( p_access, "cannot open file '%s' (%s)",
412 p_sys->psz_file, strerror(errno) );
413 free( p_sys->psz_file );
414 p_sys->psz_file = NULL;
415 p_sys->b_dump = VLC_FALSE;
419 Notify( p_access, VLC_TRUE );
425 if( ( i_write = fwrite( p_buffer, 1, i_buffer, p_sys->f ) ) > 0 )
426 p_sys->i_size += i_write;