]> git.sesse.net Git - vlc/blob - modules/access_filter/record.c
vlc security: As i've seen very little improvement on that front, i've decided to...
[vlc] / modules / access_filter / record.c
1 /*****************************************************************************
2  * record.c
3  *****************************************************************************
4  * Copyright (C) 2005-2006 the VideoLAN team
5  * $Id$
6  *
7  * Author: Laurent Aimar <fenrir@via.ecp.fr>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27
28 #include <vlc/vlc.h>
29
30 #include <vlc_input.h>
31 #include <vlc_access.h>
32
33 #include "vlc_keys.h"
34 #include <vlc_osd.h>
35 #include <vlc_charset.h>
36 #include <errno.h>
37 #include <time.h>
38
39 /*****************************************************************************
40  * Module descriptor
41  *****************************************************************************/
42
43 #define RECORD_PATH_TXT N_("Record directory")
44 #define RECORD_PATH_LONGTXT N_( \
45     "Directory where the record will be stored." )
46
47 static int  Open ( vlc_object_t * );
48 static void Close( vlc_object_t * );
49
50 vlc_module_begin();
51     set_shortname( _("Record") );
52     set_description( _("Record") );
53     set_category( CAT_INPUT );
54     set_subcategory( SUBCAT_INPUT_ACCESS_FILTER );
55     set_capability( "access_filter", 0 );
56     add_shortcut( "record" );
57
58     add_directory( "record-path", NULL, NULL,
59                    RECORD_PATH_TXT, RECORD_PATH_LONGTXT, VLC_TRUE );
60         change_unsafe();
61
62     set_callbacks( Open, Close );
63
64 vlc_module_end();
65
66 /*****************************************************************************
67  * Local prototypes
68  *****************************************************************************/
69
70 static block_t *Block  ( access_t * );
71 static ssize_t  Read   ( access_t *, uint8_t *, size_t );
72 static int      Control( access_t *, int i_query, va_list args );
73 static int      Seek   ( access_t *, int64_t );
74
75 static void Dump( access_t *, uint8_t *, int );
76
77 static int EventKey( vlc_object_t *, char const *,
78                      vlc_value_t, vlc_value_t, void * );
79
80 struct access_sys_t
81 {
82     vlc_bool_t b_dump;
83
84     char *psz_path;
85     const char *psz_ext;
86     char *psz_file;
87     int64_t i_size;
88     FILE *f;
89
90     vout_thread_t *p_vout;
91     int            i_vout_chan;
92
93     int i_update_sav;
94 };
95
96 static inline void PreUpdateFlags( access_t *p_access )
97 {
98     access_t *p_src = p_access->p_source;
99     /* backport flags turned off 0 */
100     p_src->info.i_update &= p_access->p_sys->i_update_sav ^ (~p_access->info.i_update);
101 }
102
103 static inline void PostUpdateFlags( access_t *p_access )
104 {
105     access_t *p_src = p_access->p_source;
106     /* */
107     p_access->info = p_src->info;
108     p_access->p_sys->i_update_sav = p_access->info.i_update;
109 }
110
111
112 /*****************************************************************************
113  * Open:
114  *****************************************************************************/
115 static int Open( vlc_object_t *p_this )
116 {
117     access_t *p_access = (access_t*)p_this;
118     access_t *p_src = p_access->p_source;
119     access_sys_t *p_sys;
120     char *psz;
121
122     /* */
123     p_access->pf_read  = p_src->pf_read  ? Read : NULL;
124     p_access->pf_block = p_src->pf_block ? Block : NULL;
125     p_access->pf_seek  = p_src->pf_seek  ? Seek : NULL;
126     p_access->pf_control = Control;
127
128     /* */
129     p_access->info = p_src->info;
130
131     /* */
132     p_access->p_sys = p_sys = malloc( sizeof( access_t ) );
133
134     /* */
135     p_sys->f = NULL;
136     p_sys->i_size = 0;
137     p_sys->psz_file = NULL;
138     p_sys->psz_ext = "dat";
139     p_sys->b_dump = VLC_FALSE;
140     p_sys->p_vout = NULL;
141     p_sys->i_vout_chan = -1;
142     p_sys->i_update_sav = p_access->info.i_update;
143
144     if( !strncasecmp( p_src->psz_access, "dvb", 3 ) ||
145         !strncasecmp( p_src->psz_access, "udp", 3 ) )
146         p_sys->psz_ext = "ts";
147
148     psz = var_CreateGetString( p_access, "record-path" );
149     if( *psz == '\0' )
150     {
151         free( psz );
152         if( p_access->p_libvlc->psz_homedir ) /* XXX: This should never happen */
153             psz = strdup( p_access->p_libvlc->psz_homedir );
154     }
155     p_sys->psz_path = psz;
156     msg_Dbg( p_access, "Record access filter path %s", psz );
157
158     /* catch all key event */
159     var_AddCallback( p_access->p_libvlc, "key-pressed", EventKey, p_access );
160
161     return VLC_SUCCESS;
162 }
163
164 /*****************************************************************************
165  * Close:
166  *****************************************************************************/
167 static void Close( vlc_object_t *p_this )
168 {
169     access_t     *p_access = (access_t*)p_this;
170     access_sys_t *p_sys = p_access->p_sys;
171
172     var_DelCallback( p_access->p_libvlc, "key-pressed", EventKey, p_access );
173
174     if( p_sys->f )
175     {
176         fclose( p_sys->f );
177         free( p_sys->psz_file );
178     }
179
180     free( p_sys->psz_path );
181     free( p_sys );
182 }
183
184 /*****************************************************************************
185  *
186  *****************************************************************************/
187 static block_t *Block( access_t *p_access )
188 {
189     access_t     *p_src = p_access->p_source;
190     block_t      *p_block;
191
192     /* */
193     PreUpdateFlags( p_access );
194
195     /* */
196     p_block = p_src->pf_block( p_src );
197     if( p_block && p_block->i_buffer )
198         Dump( p_access, p_block->p_buffer, p_block->i_buffer );
199
200     /* */
201     PostUpdateFlags( p_access );
202
203     return p_block;
204 }
205
206 /*****************************************************************************
207  *
208  *****************************************************************************/
209 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
210 {
211     access_t     *p_src = p_access->p_source;
212     int i_ret;
213
214     /* */
215     PreUpdateFlags( p_access );
216
217     /* */
218     i_ret = p_src->pf_read( p_src, p_buffer, i_len );
219
220     if( i_ret > 0 )
221         Dump( p_access, p_buffer, i_ret );
222
223     /* */
224     PostUpdateFlags( p_access );
225
226     return i_ret;
227 }
228
229 /*****************************************************************************
230  *
231  *****************************************************************************/
232 static int Control( access_t *p_access, int i_query, va_list args )
233 {
234     access_t     *p_src = p_access->p_source;
235     int i_ret;
236
237     /* */
238     PreUpdateFlags( p_access );
239
240     /* */
241     i_ret = p_src->pf_control( p_src, i_query, args );
242
243     /* */
244     PostUpdateFlags( p_access );
245
246     return i_ret;
247 }
248
249 /*****************************************************************************
250  *
251  *****************************************************************************/
252 static int Seek( access_t *p_access, int64_t i_pos )
253 {
254     access_t     *p_src = p_access->p_source;
255     int i_ret;
256
257     /* */
258     PreUpdateFlags( p_access );
259
260     /* */
261     i_ret = p_src->pf_seek( p_src, i_pos );
262
263     /* */
264     PostUpdateFlags( p_access );
265
266     return i_ret;
267 }
268
269 /*****************************************************************************
270  *
271  *****************************************************************************/
272 static int EventKey( vlc_object_t *p_this, char const *psz_var,
273                      vlc_value_t oldval, vlc_value_t newval, void *p_data )
274 {
275     access_t     *p_access = p_data;
276     access_sys_t *p_sys = p_access->p_sys;
277
278     struct hotkey *p_hotkeys = p_access->p_libvlc->p_hotkeys;
279     int i_action = -1, i;
280
281     for( i = 0; p_hotkeys[i].psz_action != NULL; i++ )
282     {
283         if( p_hotkeys[i].i_key == newval.i_int )
284         {
285             i_action = p_hotkeys[i].i_action;
286         }
287     }
288
289     if( i_action == ACTIONID_RECORD )
290     {
291         if( p_sys->b_dump )
292             p_sys->b_dump = VLC_FALSE;
293         else
294             p_sys->b_dump = VLC_TRUE;
295     }
296
297     return VLC_SUCCESS;
298 }
299
300 /*****************************************************************************
301  *
302  *****************************************************************************/
303 static void Notify( access_t *p_access, vlc_bool_t b_dump )
304 {
305     access_sys_t *p_sys = p_access->p_sys;
306     vout_thread_t *p_vout;
307
308     p_vout = vlc_object_find( p_access, VLC_OBJECT_VOUT, FIND_ANYWHERE );
309     if( !p_vout ) return;
310
311     if( p_vout != p_sys->p_vout )
312     {
313         p_sys->p_vout = p_vout;
314         if( spu_Control( p_vout->p_spu, SPU_CHANNEL_REGISTER,
315                          &p_sys->i_vout_chan  ) )
316             p_sys->i_vout_chan = -1;
317     }
318
319     if( p_sys->i_vout_chan != -1 )
320     {
321         if( b_dump )
322             vout_OSDMessage( p_vout, p_sys->i_vout_chan, _("Recording") );
323         else
324             vout_OSDMessage( p_vout, p_sys->i_vout_chan, _("Recording done") );
325     }
326     vlc_object_release( p_vout );
327 }
328
329 /*****************************************************************************
330  *
331  *****************************************************************************/
332 static void Dump( access_t *p_access, uint8_t *p_buffer, int i_buffer )
333 {
334     access_sys_t *p_sys = p_access->p_sys;
335     int i_write;
336
337     /* */
338     if( !p_sys->b_dump )
339     {
340         if( p_sys->f )
341         {
342             msg_Dbg( p_access, "dumped "I64Fd" kb (%s)",
343                      p_sys->i_size/1024, p_sys->psz_file );
344
345             Notify( p_access, VLC_FALSE );
346
347             fclose( p_sys->f );
348             p_sys->f = NULL;
349
350             free( p_sys->psz_file );
351             p_sys->psz_file = NULL;
352
353             p_sys->i_size = 0;
354         }
355         return;
356     }
357
358     /* */
359     if( !p_sys->f )
360     {
361         input_thread_t *p_input;
362         char *psz_name = NULL, *psz;
363         time_t t = time(NULL);
364         struct tm l;
365
366 #ifdef HAVE_LOCALTIME_R
367         if( !localtime_r( &t, &l ) ) memset( &l, 0, sizeof(l) );
368 #else
369         /* Grrr */
370         {
371             struct tm *p_l = localtime( &t );
372             if( p_l ) l = *p_l;
373             else memset( &l, 0, sizeof(l) );
374         }
375 #endif
376
377         p_input = vlc_object_find( p_access, VLC_OBJECT_INPUT, FIND_PARENT );
378         if( p_input )
379         {
380             input_item_t * p_item = input_GetItem( p_input );
381             vlc_mutex_lock( &p_item->lock );
382             if( p_item->psz_name )
383             {
384                 char *p = strrchr( p_item->psz_name, '/' );
385                 if( p == NULL )
386                     p = strrchr( p_item->psz_name, '\\' );
387
388                 if( p == NULL )
389                     psz_name = strdup( p_item->psz_name );
390                 else if( p[1] != '\0' )
391                     psz_name = strdup( &p[1] );
392             }
393             vlc_mutex_unlock( &p_item->lock );
394
395             vlc_object_release( p_input );
396         }
397
398         if( asprintf( &p_sys->psz_file, "%s %d-%d-%d %.2dh%.2dm%.2ds.%s",
399                       ( psz_name != NULL ) ? psz_name : "Unknown",
400                       l.tm_mday, l.tm_mon+1, l.tm_year+1900,
401                       l.tm_hour, l.tm_min, l.tm_sec,
402                       p_sys->psz_ext ) == -1 )
403             p_sys->psz_file = NULL;
404
405         free( psz_name );
406         if( p_sys->psz_file == NULL )
407         {
408             p_sys->b_dump = VLC_FALSE;
409             return;
410         }
411
412         /* Remove all forbidden characters (except (back)slashes) */
413         for( psz = p_sys->psz_file; *psz; psz++ )
414         {
415             unsigned char c = (unsigned char)*psz;
416
417             /* Even if many OS accept non printable characters, we remove
418              * them to avoid confusing users */
419             if( ( c < 32 ) || ( c == 127 ) )
420                 *psz = '_';
421 #if defined (WIN32) || defined (UNDER_CE)
422             /* Windows has a lot of forbidden characters, even if it has
423              * fewer than DOS. */
424             if( strchr( "\"*:<>?|", c ) != NULL )
425                 *psz = '_';
426 #endif
427         }
428
429         psz_name = p_sys->psz_file;
430
431 #if defined (WIN32) || defined (UNDER_CE)
432 #define DIR_SEP "\\"
433 #else
434 #define DIR_SEP "/"
435 #endif
436         if( asprintf( &p_sys->psz_file, "%s" DIR_SEP "%s",
437                       p_sys->psz_path, psz_name ) == -1 )
438             p_sys->psz_file = NULL;
439         free( psz_name );
440         if( p_sys->psz_file == NULL )
441         {
442             p_sys->b_dump = VLC_FALSE;
443             return;
444         }
445
446         msg_Dbg( p_access, "dump in file '%s'", p_sys->psz_file );
447
448         p_sys->f = utf8_fopen( p_sys->psz_file, "wb" );
449         if( p_sys->f == NULL )
450         {
451             msg_Err( p_access, "cannot open file '%s' (%m)",
452                      p_sys->psz_file );
453             free( p_sys->psz_file );
454             p_sys->psz_file = NULL;
455             p_sys->b_dump = VLC_FALSE;
456             return;
457         }
458
459         Notify( p_access, VLC_TRUE );
460
461         p_sys->i_size = 0;
462     }
463
464     /* */
465     if( ( i_write = fwrite( p_buffer, 1, i_buffer, p_sys->f ) ) > 0 )
466         p_sys->i_size += i_write;
467 }
468