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, 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 = 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-action", 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-action", 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;
285 if( newval.i_int == ACTIONID_RECORD )
288 p_sys->b_dump = false;
290 p_sys->b_dump = true;
296 /*****************************************************************************
298 *****************************************************************************/
299 static void Notify( access_t *p_access, bool b_dump )
301 access_sys_t *p_sys = p_access->p_sys;
302 vout_thread_t *p_vout;
304 p_vout = vlc_object_find( p_access, VLC_OBJECT_VOUT, FIND_ANYWHERE );
305 if( !p_vout ) return;
307 if( p_vout != p_sys->p_vout )
309 p_sys->p_vout = p_vout;
310 if( spu_Control( p_vout->p_spu, SPU_CHANNEL_REGISTER,
311 &p_sys->i_vout_chan ) )
312 p_sys->i_vout_chan = -1;
315 if( p_sys->i_vout_chan != -1 )
318 vout_OSDMessage( p_vout, p_sys->i_vout_chan, _("Recording") );
320 vout_OSDMessage( p_vout, p_sys->i_vout_chan, _("Recording done") );
322 vlc_object_release( p_vout );
325 /*****************************************************************************
327 *****************************************************************************/
328 static void Dump( access_t *p_access, uint8_t *p_buffer, int i_buffer )
330 access_sys_t *p_sys = p_access->p_sys;
338 msg_Dbg( p_access, "dumped %"PRId64" kb (%s)",
339 p_sys->i_size/1024, p_sys->psz_file );
341 Notify( p_access, false );
346 free( p_sys->psz_file );
347 p_sys->psz_file = NULL;
357 input_thread_t *p_input;
358 char *psz_name = NULL, *psz;
359 time_t t = time(NULL);
362 if( !localtime_r( &t, &l ) ) memset( &l, 0, sizeof(l) );
364 p_input = vlc_object_find( p_access, VLC_OBJECT_INPUT, FIND_PARENT );
367 input_item_t * p_item = input_GetItem( p_input );
368 vlc_mutex_lock( &p_item->lock );
369 if( p_item->psz_name )
371 char *p = strrchr( p_item->psz_name, '/' );
373 p = strrchr( p_item->psz_name, '\\' );
376 psz_name = strdup( p_item->psz_name );
377 else if( p[1] != '\0' )
378 psz_name = strdup( &p[1] );
380 vlc_mutex_unlock( &p_item->lock );
382 vlc_object_release( p_input );
385 if( asprintf( &p_sys->psz_file, "%s %d-%d-%d %.2dh%.2dm%.2ds.%s",
386 ( psz_name != NULL ) ? psz_name : "Unknown",
387 l.tm_mday, l.tm_mon+1, l.tm_year+1900,
388 l.tm_hour, l.tm_min, l.tm_sec,
389 p_sys->psz_ext ) == -1 )
390 p_sys->psz_file = NULL;
393 if( p_sys->psz_file == NULL )
395 p_sys->b_dump = false;
399 /* Remove all forbidden characters (except (back)slashes) */
400 for( psz = p_sys->psz_file; *psz; psz++ )
402 unsigned char c = (unsigned char)*psz;
404 /* Even if many OS accept non printable characters, we remove
405 * them to avoid confusing users */
406 if( ( c < 32 ) || ( c == 127 ) )
408 #if defined (WIN32) || defined (UNDER_CE)
409 /* Windows has a lot of forbidden characters, even if it has
411 if( strchr( "\"*:<>?|", c ) != NULL )
416 psz_name = p_sys->psz_file;
418 #if defined (WIN32) || defined (UNDER_CE)
423 if( asprintf( &p_sys->psz_file, "%s" DIR_SEP "%s",
424 p_sys->psz_path, psz_name ) == -1 )
425 p_sys->psz_file = NULL;
427 if( p_sys->psz_file == NULL )
429 p_sys->b_dump = false;
433 msg_Dbg( p_access, "dump in file '%s'", p_sys->psz_file );
435 p_sys->f = utf8_fopen( p_sys->psz_file, "wb" );
436 if( p_sys->f == NULL )
438 msg_Err( p_access, "cannot open file '%s' (%m)",
440 free( p_sys->psz_file );
441 p_sys->psz_file = NULL;
442 p_sys->b_dump = false;
446 Notify( p_access, true );
452 if( ( i_write = fwrite( p_buffer, 1, i_buffer, p_sys->f ) ) > 0 )
453 p_sys->i_size += i_write;