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