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