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