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