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