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