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