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