]> git.sesse.net Git - vlc/blob - modules/access_filter/record.c
More
[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
33 #include <vlc/input.h>
34 #include <vlc/vout.h>
35
36 #include "vlc_keys.h"
37 #include <vlc_osd.h>
38 #include "charset.h"
39 #include <errno.h>
40 #include <time.h>
41
42 /*****************************************************************************
43  * Module descriptor
44  *****************************************************************************/
45
46 #define RECORD_PATH_TXT N_("Record directory")
47 #define RECORD_PATH_LONGTXT N_( \
48     "Directory where the record will be stored." )
49
50 static int  Open ( vlc_object_t * );
51 static void Close( vlc_object_t * );
52
53 vlc_module_begin();
54     set_shortname( _("Record") );
55     set_description( _("Record") );
56     set_category( CAT_INPUT );
57     set_subcategory( SUBCAT_INPUT_ACCESS_FILTER );
58     set_capability( "access_filter", 0 );
59     add_shortcut( "record" );
60
61     add_directory( "record-path", NULL, NULL,
62                    RECORD_PATH_TXT, RECORD_PATH_LONGTXT, VLC_TRUE );
63
64     set_callbacks( Open, Close );
65
66 vlc_module_end();
67
68 /*****************************************************************************
69  * Local prototypes
70  *****************************************************************************/
71
72 static block_t *Block  ( access_t * );
73 static int      Read   ( access_t *, uint8_t *, int );
74 static int      Control( access_t *, int i_query, va_list args );
75 static int      Seek   ( access_t *, int64_t );
76
77 static void Dump( access_t *, uint8_t *, int );
78
79 static int EventKey( vlc_object_t *, char const *,
80                      vlc_value_t, vlc_value_t, void * );
81
82 struct access_sys_t
83 {
84     vlc_bool_t b_dump;
85
86     char *psz_path;
87     char *psz_ext;
88     char *psz_file;
89     int64_t i_size;
90     FILE *f;
91
92     vout_thread_t *p_vout;
93     int            i_vout_chan;
94
95     int i_update_sav;
96 };
97
98 static inline void PreUpdateFlags( access_t *p_access )
99 {
100     access_t *p_src = p_access->p_source;
101     /* backport flags turned off 0 */
102     p_src->info.i_update &= p_access->p_sys->i_update_sav ^ (~p_access->info.i_update);
103 }
104
105 static inline void PostUpdateFlags( access_t *p_access )
106 {
107     access_t *p_src = p_access->p_source;
108     /* */
109     p_access->info = p_src->info;
110     p_access->p_sys->i_update_sav = p_access->info.i_update;
111 }
112
113
114 /*****************************************************************************
115  * Open:
116  *****************************************************************************/
117 static int Open( vlc_object_t *p_this )
118 {
119     access_t *p_access = (access_t*)p_this;
120     access_t *p_src = p_access->p_source;
121     access_sys_t *p_sys;
122     char *psz;
123
124     /* */
125     p_access->pf_read  = p_src->pf_read  ? Read : NULL;
126     p_access->pf_block = p_src->pf_block ? Block : NULL;
127     p_access->pf_seek  = p_src->pf_seek  ? Seek : NULL;
128     p_access->pf_control = Control;
129
130     /* */
131     p_access->info = p_src->info;
132
133     /* */
134     p_access->p_sys = p_sys = malloc( sizeof( access_t ) );
135
136     /* */
137     p_sys->f = NULL;
138     p_sys->i_size = 0;
139     p_sys->psz_file = NULL;
140     p_sys->psz_ext = "dat";
141     p_sys->b_dump = VLC_FALSE;
142     p_sys->p_vout = NULL;
143     p_sys->i_vout_chan = -1;
144     p_sys->i_update_sav = p_access->info.i_update;
145
146     if( !strncasecmp( p_src->psz_access, "dvb", 3 ) ||
147         !strncasecmp( p_src->psz_access, "udp", 3 ) )
148         p_sys->psz_ext = "ts";
149
150     psz = var_CreateGetString( p_access, "record-path" );
151     if( *psz == '\0' )
152     {
153         free( psz );
154         if( p_access->p_vlc->psz_homedir )
155             psz = strdup( p_access->p_vlc->psz_homedir );
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_vlc, "key-pressed", 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_vlc, "key-pressed", 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     /* */
195     PreUpdateFlags( p_access );
196
197     /* */
198     p_block = p_src->pf_block( p_src );
199     if( p_block && p_block->i_buffer )
200         Dump( p_access, p_block->p_buffer, p_block->i_buffer );
201
202     /* */
203     PostUpdateFlags( p_access );
204
205     return p_block;
206 }
207
208 /*****************************************************************************
209  *
210  *****************************************************************************/
211 static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
212 {
213     access_t     *p_src = p_access->p_source;
214     int i_ret;
215
216     /* */
217     PreUpdateFlags( p_access );
218
219     /* */
220     i_ret = p_src->pf_read( p_src, p_buffer, i_len );
221
222     if( i_ret > 0 )
223         Dump( p_access, p_buffer, i_ret );
224
225     /* */
226     PostUpdateFlags( p_access );
227
228     return i_ret;
229 }
230
231 /*****************************************************************************
232  *
233  *****************************************************************************/
234 static int Control( access_t *p_access, int i_query, va_list args )
235 {
236     access_t     *p_src = p_access->p_source;
237     int i_ret;
238
239     /* */
240     PreUpdateFlags( p_access );
241
242     /* */
243     i_ret = p_src->pf_control( p_src, i_query, args );
244
245     /* */
246     PostUpdateFlags( p_access );
247
248     return i_ret;
249 }
250
251 /*****************************************************************************
252  *
253  *****************************************************************************/
254 static int Seek( access_t *p_access, int64_t i_pos )
255 {
256     access_t     *p_src = p_access->p_source;
257     int i_ret;
258
259     /* */
260     PreUpdateFlags( p_access );
261
262     /* */
263     i_ret = p_src->pf_seek( p_src, i_pos );
264
265     /* */
266     PostUpdateFlags( p_access );
267
268     return i_ret;
269 }
270
271 /*****************************************************************************
272  *
273  *****************************************************************************/
274 static int EventKey( vlc_object_t *p_this, char const *psz_var,
275                      vlc_value_t oldval, vlc_value_t newval, void *p_data )
276 {
277     access_t     *p_access = p_data;
278     access_sys_t *p_sys = p_access->p_sys;
279
280     struct hotkey *p_hotkeys = p_access->p_vlc->p_hotkeys;
281     int i_action = -1, i;
282
283     for( i = 0; p_hotkeys[i].psz_action != NULL; i++ )
284     {
285         if( p_hotkeys[i].i_key == newval.i_int )
286         {
287             i_action = p_hotkeys[i].i_action;
288         }
289     }
290
291     if( i_action == ACTIONID_RECORD )
292     {
293         if( p_sys->b_dump )
294             p_sys->b_dump = VLC_FALSE;
295         else
296             p_sys->b_dump = VLC_TRUE;
297     }
298
299     return VLC_SUCCESS;
300 }
301
302 /*****************************************************************************
303  *
304  *****************************************************************************/
305 static void Notify( access_t *p_access, vlc_bool_t b_dump )
306 {
307     access_sys_t *p_sys = p_access->p_sys;
308     vout_thread_t *p_vout;
309
310     p_vout = vlc_object_find( p_access, VLC_OBJECT_VOUT, FIND_ANYWHERE );
311     if( !p_vout ) return;
312
313     if( p_vout != p_sys->p_vout )
314     {
315         p_sys->p_vout = p_vout;
316         if( spu_Control( p_vout->p_spu, SPU_CHANNEL_REGISTER,
317                          &p_sys->i_vout_chan  ) )
318             p_sys->i_vout_chan = -1;
319     }
320
321     if( p_sys->i_vout_chan != -1 )
322     {
323         if( b_dump )
324             vout_OSDMessage( p_vout, p_sys->i_vout_chan, "Recording" );
325         else
326             vout_OSDMessage( p_vout, p_sys->i_vout_chan, "Recording done" );
327     }
328     vlc_object_release( p_vout );
329 }
330
331 /*****************************************************************************
332  *
333  *****************************************************************************/
334 static void Dump( access_t *p_access, uint8_t *p_buffer, int i_buffer )
335 {
336     access_sys_t *p_sys = p_access->p_sys;
337     int i_write;
338
339     /* */
340     if( !p_sys->b_dump )
341     {
342         if( p_sys->f )
343         {
344             msg_Dbg( p_access, "dumped "I64Fd" kb (%s)",
345                      p_sys->i_size/1024, p_sys->psz_file );
346
347             Notify( p_access, VLC_FALSE );
348
349             fclose( p_sys->f );
350             p_sys->f = NULL;
351
352             free( p_sys->psz_file );
353             p_sys->psz_file = NULL;
354
355             p_sys->i_size = 0;
356         }
357         return;
358     }
359
360     /* */
361     if( !p_sys->f )
362     {
363         input_thread_t *p_input;
364         char *psz_name = NULL, *psz;
365         time_t t = time(NULL);
366         struct tm l;
367
368 #ifdef HAVE_LOCALTIME_R
369         if( !localtime_r( &t, &l ) ) memset( &l, 0, sizeof(l) );
370 #else
371         /* Grrr */
372         {
373             struct tm *p_l = localtime( &t );
374             if( p_l ) l = *p_l;
375             else memset( &l, 0, sizeof(l) );
376         }
377 #endif
378
379         p_input = vlc_object_find( p_access, VLC_OBJECT_INPUT, FIND_PARENT );
380         if( p_input )
381         {
382             vlc_mutex_lock( &p_input->input.p_item->lock );
383             if( p_input->input.p_item->psz_name )
384             {
385                 char *p = strrchr( p_input->input.p_item->psz_name, '/' );
386                 if( p == NULL )
387                     p = strrchr( p_input->input.p_item->psz_name, '\\' );
388
389                 if( p == NULL )
390                     psz_name = strdup( p_input->input.p_item->psz_name );
391                 else if( p[1] != '\0' )
392                     psz_name = strdup( &p[1] );
393             }
394             vlc_mutex_unlock( &p_input->input.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