]> git.sesse.net Git - vlc/blob - modules/stream_filter/record.c
Removed useless ACCESS_GET_MTU/STREAM_GET_MTU.
[vlc] / modules / stream_filter / record.c
1 /*****************************************************************************
2  * record.c
3  *****************************************************************************
4  * Copyright (C) 2008 Laurent Aimar
5  * $Id$
6  *
7  * Author: Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
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 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33
34 #include <assert.h>
35 #include <vlc_stream.h>
36 #include <vlc_input.h>
37 #include <vlc_charset.h>
38
39
40 /*****************************************************************************
41  * Module descriptor
42  *****************************************************************************/
43 static int  Open ( vlc_object_t * );
44 static void Close( vlc_object_t * );
45
46 vlc_module_begin()
47     set_category( CAT_INPUT )
48     set_subcategory( SUBCAT_INPUT_STREAM_FILTER )
49     set_description( N_("Internal stream record") )
50     set_capability( "stream_filter", 0 )
51     set_callbacks( Open, Close )
52 vlc_module_end()
53
54 /*****************************************************************************
55  *
56  *****************************************************************************/
57 struct stream_sys_t
58 {
59     bool b_active;
60
61     FILE *f;        /* TODO it could be replaced by access_output_t one day */
62     bool b_error;
63 };
64
65
66 /****************************************************************************
67  * Local prototypes
68  ****************************************************************************/
69 static int  Read   ( stream_t *, void *p_read, unsigned int i_read );
70 static int  Peek   ( stream_t *, const uint8_t **pp_peek, unsigned int i_peek );
71 static int  Control( stream_t *, int i_query, va_list );
72
73 static int  Start  ( stream_t *, const char *psz_extension );
74 static int  Stop   ( stream_t * );
75 static void Write  ( stream_t *, const uint8_t *p_buffer, size_t i_buffer );
76
77 /****************************************************************************
78  * Open
79  ****************************************************************************/
80 static int Open ( vlc_object_t *p_this )
81 {
82     stream_t *s = (stream_t*)p_this;
83     stream_sys_t *p_sys;
84
85     /* */
86     s->p_sys = p_sys = malloc( sizeof( *p_sys ) );
87     if( !p_sys )
88         return VLC_ENOMEM;
89
90     p_sys->b_active = false;
91
92     /* */
93     s->pf_read = Read;
94     s->pf_peek = Peek;
95     s->pf_control = Control;
96
97     return VLC_SUCCESS;
98 }
99
100 /****************************************************************************
101  * Close
102  ****************************************************************************/
103 static void Close( vlc_object_t *p_this )
104 {
105     stream_t *s = (stream_t*)p_this;
106     stream_sys_t *p_sys = s->p_sys;
107
108     if( p_sys->b_active )
109         Stop( s );
110
111     free( p_sys );
112 }
113
114 /****************************************************************************
115  * Stream filters functions
116  ****************************************************************************/
117 static int Read( stream_t *s, void *p_read, unsigned int i_read )
118 {
119     stream_sys_t *p_sys = s->p_sys;
120     void *p_record = p_read;
121
122     /* Allocate a temporary buffer for record when no p_read */
123     if( p_sys->b_active && !p_record )
124         p_record = malloc( i_read );
125
126     /* */
127     const int i_record = stream_Read( s->p_source, p_record, i_read );
128
129     /* Dump read data */
130     if( p_sys->b_active )
131     {
132         if( p_record && i_record > 0 )
133             Write( s, p_record, i_record );
134         if( !p_read )
135             free( p_record );
136     }
137
138     return i_record;
139 }
140
141 static int Peek( stream_t *s, const uint8_t **pp_peek, unsigned int i_peek )
142 {
143     return stream_Peek( s->p_source, pp_peek, i_peek );
144 }
145
146 static int Control( stream_t *s, int i_query, va_list args )
147 {
148     if( i_query != STREAM_SET_RECORD_STATE )
149         return stream_vaControl( s->p_source, i_query, args );
150
151     bool b_active = (bool)va_arg( args, int );
152     const char *psz_extension = NULL;
153     if( b_active )
154         psz_extension = (const char*)va_arg( args, const char* );
155
156     if( !s->p_sys->b_active == !b_active )
157         return VLC_SUCCESS;
158
159     if( b_active )
160         return Start( s, psz_extension );
161     else
162         return Stop( s );
163 }
164
165 /****************************************************************************
166  * Helpers
167  ****************************************************************************/
168 static int Start( stream_t *s, const char *psz_extension )
169 {
170     stream_sys_t *p_sys = s->p_sys;
171
172     char *psz_file;
173     FILE *f;
174
175     /* */
176     if( !psz_extension )
177         psz_extension = "dat";
178
179     /* Retreive path */
180     char *psz_path = var_CreateGetString( s, "input-record-path" );
181     if( !psz_path || *psz_path == '\0' )
182     {
183         free( psz_path );
184         psz_path = strdup( config_GetHomeDir() );
185     }
186
187     if( !psz_path )
188         return VLC_ENOMEM;
189
190     /* Create file name
191      * TODO allow prefix configuration */
192     psz_file = input_CreateFilename( VLC_OBJECT(s), psz_path, INPUT_RECORD_PREFIX, psz_extension );
193
194     free( psz_path );
195
196     if( !psz_file )
197         return VLC_ENOMEM;
198
199     f = utf8_fopen( psz_file, "wb" );
200     if( !f )
201     {
202         free( psz_file );
203         return VLC_EGENERIC;
204     }
205     msg_Dbg( s, "Recording into %s", psz_file );
206     free( psz_file );
207
208     /* */
209     p_sys->f = f;
210     p_sys->b_active = true;
211     p_sys->b_error = false;
212     return VLC_SUCCESS;
213 }
214 static int Stop( stream_t *s )
215 {
216     stream_sys_t *p_sys = s->p_sys;
217
218     assert( p_sys->b_active );
219
220     msg_Dbg( s, "Recording completed" );
221     fclose( p_sys->f );
222     p_sys->b_active = false;
223     return VLC_SUCCESS;
224 }
225
226 static void Write( stream_t *s, const uint8_t *p_buffer, size_t i_buffer )
227 {
228     stream_sys_t *p_sys = s->p_sys;
229
230     assert( p_sys->b_active );
231
232     if( i_buffer > 0 )
233     {
234         const bool b_previous_error = p_sys->b_error;
235         const size_t i_written = fwrite( p_buffer, 1, i_buffer, p_sys->f );
236
237         p_sys->b_error = i_written != i_buffer;
238
239         /* TODO maybe a intf_UserError or something like that ? */
240         if( p_sys->b_error && !b_previous_error )
241             msg_Err( s, "Failed to record data (begin)" );
242         else if( !p_sys->b_error && b_previous_error )
243             msg_Err( s, "Failed to record data (end)" );
244     }
245 }