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_plugin.h>
35 #include <vlc_input.h>
36 #include <vlc_access.h>
40 #include <vlc_charset.h>
44 /*****************************************************************************
46 *****************************************************************************/
48 #define RECORD_PATH_TXT N_("Record directory")
49 #define RECORD_PATH_LONGTXT N_( \
50 "Directory where the record will be stored." )
52 static int Open ( vlc_object_t * );
53 static void Close( vlc_object_t * );
56 set_shortname( _("Record") );
57 set_description( _("Record") );
58 set_category( CAT_INPUT );
59 set_subcategory( SUBCAT_INPUT_ACCESS_FILTER );
60 set_capability( "access_filter", 0 );
61 add_shortcut( "record" );
63 add_directory( "record-path", NULL, NULL,
64 RECORD_PATH_TXT, RECORD_PATH_LONGTXT, true );
67 set_callbacks( Open, Close );
71 /*****************************************************************************
73 *****************************************************************************/
75 static block_t *Block ( access_t * );
76 static ssize_t Read ( access_t *, uint8_t *, size_t );
77 static int Control( access_t *, int i_query, va_list args );
78 static int Seek ( access_t *, int64_t );
80 static void Dump( access_t *, uint8_t *, int );
82 static int EventKey( vlc_object_t *, char const *,
83 vlc_value_t, vlc_value_t, void * );
95 vout_thread_t *p_vout;
101 static inline void PreUpdateFlags( access_t *p_access )
103 access_t *p_src = p_access->p_source;
104 /* backport flags turned off 0 */
105 p_src->info.i_update &= p_access->p_sys->i_update_sav ^ (~p_access->info.i_update);
108 static inline void PostUpdateFlags( access_t *p_access )
110 access_t *p_src = p_access->p_source;
112 p_access->info = p_src->info;
113 p_access->p_sys->i_update_sav = p_access->info.i_update;
117 /*****************************************************************************
119 *****************************************************************************/
120 static int Open( vlc_object_t *p_this )
122 access_t *p_access = (access_t*)p_this;
123 access_t *p_src = p_access->p_source;
128 p_access->pf_read = p_src->pf_read ? Read : NULL;
129 p_access->pf_block = p_src->pf_block ? Block : NULL;
130 p_access->pf_seek = p_src->pf_seek ? Seek : NULL;
131 p_access->pf_control = Control;
134 p_access->info = p_src->info;
137 p_access->p_sys = p_sys = malloc( sizeof( access_t ) );
142 p_sys->psz_file = NULL;
143 p_sys->psz_ext = "dat";
144 p_sys->b_dump = false;
145 p_sys->p_vout = NULL;
146 p_sys->i_vout_chan = -1;
147 p_sys->i_update_sav = p_access->info.i_update;
149 if( !strncasecmp( p_src->psz_access, "dvb", 3 ) ||
150 !strncasecmp( p_src->psz_access, "udp", 3 ) )
151 p_sys->psz_ext = "ts";
153 psz = var_CreateGetString( p_access, "record-path" );
157 if( p_access->p_libvlc->psz_homedir ) /* XXX: This should never happen */
158 psz = strdup( p_access->p_libvlc->psz_homedir );
160 p_sys->psz_path = psz;
161 msg_Dbg( p_access, "Record access filter path %s", psz );
163 /* catch all key event */
164 var_AddCallback( p_access->p_libvlc, "key-action", EventKey, p_access );
169 /*****************************************************************************
171 *****************************************************************************/
172 static void Close( vlc_object_t *p_this )
174 access_t *p_access = (access_t*)p_this;
175 access_sys_t *p_sys = p_access->p_sys;
177 var_DelCallback( p_access->p_libvlc, "key-action", EventKey, p_access );
182 free( p_sys->psz_file );
185 free( p_sys->psz_path );
189 /*****************************************************************************
191 *****************************************************************************/
192 static block_t *Block( access_t *p_access )
194 access_t *p_src = p_access->p_source;
198 PreUpdateFlags( p_access );
201 p_block = p_src->pf_block( p_src );
202 if( p_block && p_block->i_buffer )
203 Dump( p_access, p_block->p_buffer, p_block->i_buffer );
206 PostUpdateFlags( p_access );
211 /*****************************************************************************
213 *****************************************************************************/
214 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
216 access_t *p_src = p_access->p_source;
220 PreUpdateFlags( p_access );
223 i_ret = p_src->pf_read( p_src, p_buffer, i_len );
226 Dump( p_access, p_buffer, i_ret );
229 PostUpdateFlags( p_access );
234 /*****************************************************************************
236 *****************************************************************************/
237 static int Control( access_t *p_access, int i_query, va_list args )
239 access_t *p_src = p_access->p_source;
243 PreUpdateFlags( p_access );
246 i_ret = p_src->pf_control( p_src, i_query, args );
249 PostUpdateFlags( p_access );
254 /*****************************************************************************
256 *****************************************************************************/
257 static int Seek( access_t *p_access, int64_t i_pos )
259 access_t *p_src = p_access->p_source;
263 PreUpdateFlags( p_access );
266 i_ret = p_src->pf_seek( p_src, i_pos );
269 PostUpdateFlags( p_access );
274 /*****************************************************************************
276 *****************************************************************************/
277 static int EventKey( vlc_object_t *p_this, char const *psz_var,
278 vlc_value_t oldval, vlc_value_t newval, void *p_data )
280 access_t *p_access = p_data;
281 access_sys_t *p_sys = p_access->p_sys;
286 if( newval.i_int == ACTIONID_RECORD )
289 p_sys->b_dump = false;
291 p_sys->b_dump = true;
297 /*****************************************************************************
299 *****************************************************************************/
300 static void Notify( access_t *p_access, bool b_dump )
302 access_sys_t *p_sys = p_access->p_sys;
303 vout_thread_t *p_vout;
305 p_vout = vlc_object_find( p_access, VLC_OBJECT_VOUT, FIND_ANYWHERE );
306 if( !p_vout ) return;
308 if( p_vout != p_sys->p_vout )
310 p_sys->p_vout = p_vout;
311 if( spu_Control( p_vout->p_spu, SPU_CHANNEL_REGISTER,
312 &p_sys->i_vout_chan ) )
313 p_sys->i_vout_chan = -1;
316 if( p_sys->i_vout_chan != -1 )
319 vout_OSDMessage( p_vout, p_sys->i_vout_chan, _("Recording") );
321 vout_OSDMessage( p_vout, p_sys->i_vout_chan, _("Recording done") );
323 vlc_object_release( p_vout );
326 /*****************************************************************************
328 *****************************************************************************/
329 static void Dump( access_t *p_access, uint8_t *p_buffer, int i_buffer )
331 access_sys_t *p_sys = p_access->p_sys;
339 msg_Dbg( p_access, "dumped %"PRId64" kb (%s)",
340 p_sys->i_size/1024, p_sys->psz_file );
342 Notify( p_access, false );
347 free( p_sys->psz_file );
348 p_sys->psz_file = NULL;
358 input_thread_t *p_input;
359 char *psz_name = NULL, *psz;
360 time_t t = time(NULL);
363 if( !localtime_r( &t, &l ) ) memset( &l, 0, sizeof(l) );
365 p_input = vlc_object_find( p_access, VLC_OBJECT_INPUT, FIND_PARENT );
368 input_item_t * p_item = input_GetItem( p_input );
369 vlc_mutex_lock( &p_item->lock );
370 if( p_item->psz_name )
372 char *p = strrchr( p_item->psz_name, '/' );
374 p = strrchr( p_item->psz_name, '\\' );
377 psz_name = strdup( p_item->psz_name );
378 else if( p[1] != '\0' )
379 psz_name = strdup( &p[1] );
381 vlc_mutex_unlock( &p_item->lock );
383 vlc_object_release( p_input );
386 if( asprintf( &p_sys->psz_file, "%s %d-%d-%d %.2dh%.2dm%.2ds.%s",
387 ( psz_name != NULL ) ? psz_name : "Unknown",
388 l.tm_mday, l.tm_mon+1, l.tm_year+1900,
389 l.tm_hour, l.tm_min, l.tm_sec,
390 p_sys->psz_ext ) == -1 )
391 p_sys->psz_file = NULL;
394 if( p_sys->psz_file == NULL )
396 p_sys->b_dump = false;
400 /* Remove all forbidden characters (except (back)slashes) */
401 for( psz = p_sys->psz_file; *psz; psz++ )
403 unsigned char c = (unsigned char)*psz;
405 /* Even if many OS accept non printable characters, we remove
406 * them to avoid confusing users */
407 if( ( c < 32 ) || ( c == 127 ) )
409 #if defined (WIN32) || defined (UNDER_CE)
410 /* Windows has a lot of forbidden characters, even if it has
412 if( strchr( "\"*:<>?|", c ) != NULL )
417 psz_name = p_sys->psz_file;
419 #if defined (WIN32) || defined (UNDER_CE)
424 if( asprintf( &p_sys->psz_file, "%s" DIR_SEP "%s",
425 p_sys->psz_path, psz_name ) == -1 )
426 p_sys->psz_file = NULL;
428 if( p_sys->psz_file == NULL )
430 p_sys->b_dump = false;
434 msg_Dbg( p_access, "dump in file '%s'", p_sys->psz_file );
436 p_sys->f = utf8_fopen( p_sys->psz_file, "wb" );
437 if( p_sys->f == NULL )
439 msg_Err( p_access, "cannot open file '%s' (%m)",
441 free( p_sys->psz_file );
442 p_sys->psz_file = NULL;
443 p_sys->b_dump = false;
447 Notify( p_access, true );
453 if( ( i_write = fwrite( p_buffer, 1, i_buffer, p_sys->f ) ) > 0 )
454 p_sys->i_size += i_write;