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