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