]> git.sesse.net Git - vlc/blob - modules/access_filter/dump.c
Use Brackets for global headers.
[vlc] / modules / access_filter / dump.c
1 /*****************************************************************************
2  * dump.c
3  *****************************************************************************
4  * Copyright © 2006 Rémi Denis-Courmont
5  * $Id$
6  *
7  * Author: Rémi Denis-Courmont <rem # videolan,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 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include <vlc_common.h>
29 #include <vlc_plugin.h>
30
31 #include <assert.h>
32 #include <time.h>
33 #include <errno.h>
34
35 #include <vlc_access.h>
36
37 #include <vlc_charset.h>
38 #include <vlc_keys.h>
39
40 #define DEFAULT_MARGIN 32 // megabytes
41
42 #define FORCE_TEXT N_("Force use of dump module")
43 #define FORCE_LONGTEXT N_("Activate the dump module " \
44                           "even for media with fast seeking.")
45
46 #define MARGIN_TEXT N_("Maximum size of temporary file (Mb)")
47 #define MARGIN_LONGTEXT N_("The dump module will abort dumping of the media " \
48                            "if more than this much megabyte were performed.")
49
50 static int  Open (vlc_object_t *);
51 static void Close (vlc_object_t *);
52
53 vlc_module_begin ()
54     set_shortname (N_("Dump"))
55     set_description (N_("Dump"))
56     set_category (CAT_INPUT)
57     set_subcategory (SUBCAT_INPUT_ACCESS_FILTER)
58     set_capability ("access_filter", 0)
59     add_shortcut ("dump")
60     set_callbacks (Open, Close)
61
62     add_bool ("dump-force", false, NULL, FORCE_TEXT,
63               FORCE_LONGTEXT, false);
64     add_integer ("dump-margin", DEFAULT_MARGIN, NULL, MARGIN_TEXT,
65                  MARGIN_LONGTEXT, false);
66 vlc_module_end ()
67
68 static ssize_t Read (access_t *access, uint8_t *buffer, size_t len);
69 static block_t *Block (access_t *access);
70 static int Seek (access_t *access, int64_t offset);
71 static int Control (access_t *access, int cmd, va_list ap);
72
73 static void Trigger (access_t *access);
74 static int KeyHandler (vlc_object_t *obj, char const *varname,
75                        vlc_value_t oldval, vlc_value_t newval, void *data);
76
77 struct access_sys_t
78 {
79     FILE *stream;
80     int64_t tmp_max;
81     int64_t dumpsize;
82 };
83
84 /**
85  * Open()
86  */
87 static int Open (vlc_object_t *obj)
88 {
89     access_t *access = (access_t*)obj;
90     access_t *src = access->p_source;
91
92     if (!var_CreateGetBool (access, "dump-force"))
93     {
94         bool b;
95         if ((access_Control (src, ACCESS_CAN_FASTSEEK, &b) == 0) && b)
96         {
97             msg_Dbg (obj, "dump filter useless");
98             return VLC_EGENERIC;
99         }
100     }
101
102     if (src->pf_read != NULL)
103         access->pf_read = Read;
104     else
105         access->pf_block = Block;
106     if (src->pf_seek != NULL)
107         access->pf_seek = Seek;
108
109     access->pf_control = Control;
110     access->info = src->info;
111
112     access_sys_t *p_sys = access->p_sys = calloc( 1, sizeof (*p_sys) );
113     if( !p_sys )
114         return VLC_ENOMEM;
115
116 # ifndef UNDER_CE
117     if ((p_sys->stream = tmpfile ()) == NULL)
118 # else
119     char buf[75];
120     if(GetTempFileName("\\Temp\\","vlc",0,buf) || ((p_sys->stream = fopen(buf,"wb+")) ==NULL))
121 #endif
122     {
123         msg_Err (access, "cannot create temporary file: %m");
124         free (p_sys);
125         return VLC_EGENERIC;
126     }
127     p_sys->tmp_max = ((int64_t)var_CreateGetInteger (access, "dump-margin")) << 20;
128
129     var_AddCallback (access->p_libvlc, "key-action", KeyHandler, access);
130
131     return VLC_SUCCESS;
132 }
133
134
135 /**
136  * Close()
137  */
138 static void Close (vlc_object_t *obj)
139 {
140     access_t *access = (access_t *)obj;
141     access_sys_t *p_sys = access->p_sys;
142
143     var_DelCallback (access->p_libvlc, "key-action", KeyHandler, access);
144
145     if (p_sys->stream != NULL)
146         fclose (p_sys->stream);
147     free (p_sys);
148 }
149
150
151 static void Dump (access_t *access, const uint8_t *buffer, size_t len)
152 {
153     access_sys_t *p_sys = access->p_sys;
154     FILE *stream = p_sys->stream;
155
156     if ((stream == NULL) /* not dumping */
157      || (access->info.i_pos < p_sys->dumpsize) /* already known data */)
158         return;
159
160     size_t needed = access->info.i_pos - p_sys->dumpsize;
161     if (len < needed)
162         return; /* gap between data and dump offset (seek too far ahead?) */
163
164     buffer += len - needed;
165     len = needed;
166
167     if (len == 0)
168         return; /* no useful data */
169
170     if ((p_sys->tmp_max != -1) && (access->info.i_pos > p_sys->tmp_max))
171     {
172         msg_Dbg (access, "too much data - dump will not work");
173         goto error;
174     }
175
176     assert (len > 0);
177     if (fwrite (buffer, len, 1, stream) != 1)
178     {
179         msg_Err (access, "cannot write to file: %m");
180         goto error;
181     }
182
183     p_sys->dumpsize += len;
184     return;
185
186 error:
187     fclose (stream);
188     p_sys->stream = NULL;
189 }
190
191
192 static ssize_t Read (access_t *access, uint8_t *buffer, size_t len)
193 {
194     access_t *src = access->p_source;
195
196     src->info.i_update = access->info.i_update;
197     len = src->pf_read (src, buffer, len);
198     access->info = src->info;
199
200     Dump (access, buffer, len);
201
202     return len;
203 }
204
205
206 static block_t *Block (access_t *access)
207 {
208     access_t *src = access->p_source;
209     block_t *block;
210
211     src->info.i_update = access->info.i_update;
212     block = src->pf_block (src);
213     access->info = src->info;
214
215     if ((block == NULL) || (block->i_buffer <= 0))
216         return block;
217
218     Dump (access, block->p_buffer, block->i_buffer);
219
220     return block;
221 }
222
223
224 static int Control (access_t *access, int cmd, va_list ap)
225 {
226     access_t *src = access->p_source;
227
228     return src->pf_control (src, cmd, ap);
229 }
230
231
232 static int Seek (access_t *access, int64_t offset)
233 {
234     access_t *src = access->p_source;
235     access_sys_t *p_sys = access->p_sys;
236
237     if (p_sys->tmp_max == -1)
238     {
239         msg_Err (access, "cannot seek while dumping!");
240         return VLC_EGENERIC;
241     }
242
243     if (p_sys->stream != NULL)
244         msg_Dbg (access, "seeking - dump might not work");
245
246     src->info.i_update = access->info.i_update;
247     int ret = src->pf_seek (src, offset);
248     access->info = src->info;
249     return ret;
250 }
251
252 static void Trigger (access_t *access)
253 {
254     access_sys_t *p_sys = access->p_sys;
255
256     if (p_sys->stream == NULL)
257         return; // too late
258
259     if (p_sys->tmp_max == -1)
260         return; // already triggered - should we stop? FIXME
261
262     time_t now;
263     time (&now);
264
265     struct tm t;
266     if (localtime_r (&now, &t) == NULL)
267         return; // No time, eh? Well, I'd rather not run on your computer.
268
269     if (t.tm_year > 999999999)
270         // Humanity is about 300 times older than when this was written,
271         // and there is an off-by-one in the following sprintf().
272         return;
273
274     const char *home = config_GetHomeDir();
275
276     /* Hmm what about the extension?? */
277     char filename[strlen (home) + sizeof ("/vlcdump-YYYYYYYYY-MM-DD-HH-MM-SS.ts")];
278     sprintf (filename, "%s/vlcdump-%04u-%02u-%02u-%02u-%02u-%02u.ts", home,
279              t.tm_year, t.tm_mon, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec);
280
281     msg_Info (access, "dumping media to \"%s\"...", filename);
282
283     FILE *newstream = utf8_fopen (filename, "wb");
284     if (newstream == NULL)
285     {
286         msg_Err (access, "cannot create dump file \"%s\": %m", filename);
287         return;
288     }
289
290     /* This might cause excessive hard drive work :( */
291     FILE *oldstream = p_sys->stream;
292     rewind (oldstream);
293
294     for (;;)
295     {
296         char buf[16384];
297         size_t len = fread (buf, 1, sizeof (buf), oldstream);
298         if (len == 0)
299         {
300             if (ferror (oldstream))
301             {
302                 msg_Err (access, "cannot read temporary file: %m");
303                 break;
304             }
305
306             /* Done with temporary file */
307             fclose (oldstream);
308             p_sys->stream = newstream;
309             p_sys->tmp_max = -1;
310             return;
311         }
312
313         if (fwrite (buf, len, 1, newstream) != 1)
314         {
315             msg_Err (access, "cannot write dump file: %m");
316             break;
317         }
318     }
319
320     /* Failed to copy temporary file */
321     fseek (oldstream, 0, SEEK_END);
322     fclose (newstream);
323     return;
324 }
325
326
327 static int KeyHandler (vlc_object_t *obj, char const *varname,
328                        vlc_value_t oldval, vlc_value_t newval, void *data)
329 {
330     access_t *access = data;
331
332     (void)varname;
333     (void)oldval;
334     (void)obj;
335
336     if (newval.i_int == ACTIONID_DUMP)
337         Trigger (access);
338     return VLC_SUCCESS;
339 }