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