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