]> git.sesse.net Git - vlc/blob - modules/access_filter/record.c
Merge branch 'master' of git@git.videolan.org:vlc
[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, 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     bool 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 = 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-action", 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-action", 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     (void)psz_var;
283     (void)oldval;
284
285     if( newval.i_int == ACTIONID_RECORD )
286     {
287         if( p_sys->b_dump )
288             p_sys->b_dump = false;
289         else
290             p_sys->b_dump = true;
291     }
292
293     return VLC_SUCCESS;
294 }
295
296 /*****************************************************************************
297  *
298  *****************************************************************************/
299 static void Notify( access_t *p_access, bool b_dump )
300 {
301     access_sys_t *p_sys = p_access->p_sys;
302     vout_thread_t *p_vout;
303
304     p_vout = vlc_object_find( p_access, VLC_OBJECT_VOUT, FIND_ANYWHERE );
305     if( !p_vout ) return;
306
307     if( p_vout != p_sys->p_vout )
308     {
309         p_sys->p_vout = p_vout;
310         if( spu_Control( p_vout->p_spu, SPU_CHANNEL_REGISTER,
311                          &p_sys->i_vout_chan  ) )
312             p_sys->i_vout_chan = -1;
313     }
314
315     if( p_sys->i_vout_chan != -1 )
316     {
317         if( b_dump )
318             vout_OSDMessage( p_vout, p_sys->i_vout_chan, _("Recording") );
319         else
320             vout_OSDMessage( p_vout, p_sys->i_vout_chan, _("Recording done") );
321     }
322     vlc_object_release( p_vout );
323 }
324
325 /*****************************************************************************
326  *
327  *****************************************************************************/
328 static void Dump( access_t *p_access, uint8_t *p_buffer, int i_buffer )
329 {
330     access_sys_t *p_sys = p_access->p_sys;
331     int i_write;
332
333     /* */
334     if( !p_sys->b_dump )
335     {
336         if( p_sys->f )
337         {
338             msg_Dbg( p_access, "dumped "I64Fd" kb (%s)",
339                      p_sys->i_size/1024, p_sys->psz_file );
340
341             Notify( p_access, false );
342
343             fclose( p_sys->f );
344             p_sys->f = NULL;
345
346             free( p_sys->psz_file );
347             p_sys->psz_file = NULL;
348
349             p_sys->i_size = 0;
350         }
351         return;
352     }
353
354     /* */
355     if( !p_sys->f )
356     {
357         input_thread_t *p_input;
358         char *psz_name = NULL, *psz;
359         time_t t = time(NULL);
360         struct tm l;
361
362         if( !localtime_r( &t, &l ) ) memset( &l, 0, sizeof(l) );
363
364         p_input = vlc_object_find( p_access, VLC_OBJECT_INPUT, FIND_PARENT );
365         if( p_input )
366         {
367             input_item_t * p_item = input_GetItem( p_input );
368             vlc_mutex_lock( &p_item->lock );
369             if( p_item->psz_name )
370             {
371                 char *p = strrchr( p_item->psz_name, '/' );
372                 if( p == NULL )
373                     p = strrchr( p_item->psz_name, '\\' );
374
375                 if( p == NULL )
376                     psz_name = strdup( p_item->psz_name );
377                 else if( p[1] != '\0' )
378                     psz_name = strdup( &p[1] );
379             }
380             vlc_mutex_unlock( &p_item->lock );
381
382             vlc_object_release( p_input );
383         }
384
385         if( asprintf( &p_sys->psz_file, "%s %d-%d-%d %.2dh%.2dm%.2ds.%s",
386                       ( psz_name != NULL ) ? psz_name : "Unknown",
387                       l.tm_mday, l.tm_mon+1, l.tm_year+1900,
388                       l.tm_hour, l.tm_min, l.tm_sec,
389                       p_sys->psz_ext ) == -1 )
390             p_sys->psz_file = NULL;
391
392         free( psz_name );
393         if( p_sys->psz_file == NULL )
394         {
395             p_sys->b_dump = false;
396             return;
397         }
398
399         /* Remove all forbidden characters (except (back)slashes) */
400         for( psz = p_sys->psz_file; *psz; psz++ )
401         {
402             unsigned char c = (unsigned char)*psz;
403
404             /* Even if many OS accept non printable characters, we remove
405              * them to avoid confusing users */
406             if( ( c < 32 ) || ( c == 127 ) )
407                 *psz = '_';
408 #if defined (WIN32) || defined (UNDER_CE)
409             /* Windows has a lot of forbidden characters, even if it has
410              * fewer than DOS. */
411             if( strchr( "\"*:<>?|", c ) != NULL )
412                 *psz = '_';
413 #endif
414         }
415
416         psz_name = p_sys->psz_file;
417
418 #if defined (WIN32) || defined (UNDER_CE)
419 #define DIR_SEP "\\"
420 #else
421 #define DIR_SEP "/"
422 #endif
423         if( asprintf( &p_sys->psz_file, "%s" DIR_SEP "%s",
424                       p_sys->psz_path, psz_name ) == -1 )
425             p_sys->psz_file = NULL;
426         free( psz_name );
427         if( p_sys->psz_file == NULL )
428         {
429             p_sys->b_dump = false;
430             return;
431         }
432
433         msg_Dbg( p_access, "dump in file '%s'", p_sys->psz_file );
434
435         p_sys->f = utf8_fopen( p_sys->psz_file, "wb" );
436         if( p_sys->f == NULL )
437         {
438             msg_Err( p_access, "cannot open file '%s' (%m)",
439                      p_sys->psz_file );
440             free( p_sys->psz_file );
441             p_sys->psz_file = NULL;
442             p_sys->b_dump = false;
443             return;
444         }
445
446         Notify( p_access, true );
447
448         p_sys->i_size = 0;
449     }
450
451     /* */
452     if( ( i_write = fwrite( p_buffer, 1, i_buffer, p_sys->f ) ) > 0 )
453         p_sys->i_size += i_write;
454 }
455