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