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