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