1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright © 2006 Rémi Denis-Courmont
7 * Author: Rémi Denis-Courmont <rem # videolan,org>
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.
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.
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 *****************************************************************************/
31 #include <vlc_access.h>
33 #include <vlc_charset.h>
36 #define DEFAULT_MARGIN 32 // megabytes
38 #define FORCE_TEXT N_("Force use of dump module")
39 #define FORCE_LONGTEXT N_("Activate the dump module " \
40 "even for media with fast seeking.")
42 #define MARGIN_TEXT N_("Maximum size of temporary file (Mb)")
43 #define MARGIN_LONGTEXT N_("The dump module will abort dumping of the media " \
44 "if more than this much megabyte were performed.")
46 static int Open (vlc_object_t *);
47 static void Close (vlc_object_t *);
50 set_shortname (_("Dump"));
51 set_description (_("Dump"));
52 set_category (CAT_INPUT);
53 set_subcategory (SUBCAT_INPUT_ACCESS_FILTER);
54 set_capability ("access_filter", 0);
55 add_shortcut ("dump");
56 set_callbacks (Open, Close);
58 add_bool ("dump-force", VLC_FALSE, NULL, FORCE_TEXT,
59 FORCE_LONGTEXT, VLC_FALSE);
60 add_integer ("dump-margin", DEFAULT_MARGIN, NULL, MARGIN_TEXT,
61 MARGIN_LONGTEXT, VLC_FALSE);
64 static int Read (access_t *access, uint8_t *buffer, int len);
65 static block_t *Block (access_t *access);
66 static int Seek (access_t *access, int64_t offset);
67 static int Control (access_t *access, int cmd, va_list ap);
69 static void Trigger (access_t *access);
70 static int KeyHandler (vlc_object_t *obj, char const *varname,
71 vlc_value_t oldval, vlc_value_t newval, void *data);
83 static int Open (vlc_object_t *obj)
85 access_t *access = (access_t*)obj;
86 access_t *src = access->p_source;
88 if (!var_CreateGetBool (access, "dump-force"))
91 if ((access2_Control (src, ACCESS_CAN_FASTSEEK, &b) == 0) && b)
93 msg_Dbg (obj, "dump filter useless");
98 if (src->pf_read != NULL)
99 access->pf_read = Read;
101 access->pf_block = Block;
102 if (src->pf_seek != NULL)
103 access->pf_seek = Seek;
105 access->pf_control = Control;
106 access->info = src->info;
108 access_sys_t *p_sys = access->p_sys = malloc (sizeof (*p_sys));
111 memset (p_sys, 0, sizeof (*p_sys));
113 if ((p_sys->stream = tmpfile ()) == NULL)
115 msg_Err (access, "cannot create temporary file: %s", strerror (errno));
119 p_sys->tmp_max = ((int64_t)var_CreateGetInteger (access, "dump-margin")) << 20;
121 var_AddCallback (access->p_libvlc, "key-pressed", KeyHandler, access);
130 static void Close (vlc_object_t *obj)
132 access_t *access = (access_t *)obj;
133 access_sys_t *p_sys = access->p_sys;
135 var_DelCallback (access->p_libvlc, "key-pressed", KeyHandler, access);
137 if (p_sys->stream != NULL)
138 fclose (p_sys->stream);
143 static void Dump (access_t *access, const uint8_t *buffer, size_t len)
145 access_sys_t *p_sys = access->p_sys;
146 FILE *stream = p_sys->stream;
148 if ((stream == NULL) /* not dumping */
149 || (access->info.i_pos < p_sys->dumpsize) /* already known data */)
152 size_t needed = access->info.i_pos - p_sys->dumpsize;
154 return; /* gap between data and dump offset (seek too far ahead?) */
156 buffer += len - needed;
160 return; /* no useful data */
162 if ((p_sys->tmp_max != -1) && (access->info.i_pos > p_sys->tmp_max))
164 msg_Dbg (access, "too much data - dump will not work");
169 if (fwrite (buffer, len, 1, stream) != 1)
171 msg_Err (access, "cannot write to file: %s", strerror (errno));
175 p_sys->dumpsize += len;
180 p_sys->stream = NULL;
184 static int Read (access_t *access, uint8_t *buffer, int len)
186 access_t *src = access->p_source;
188 src->info.i_update = access->info.i_update;
189 len = src->pf_read (src, buffer, len);
190 access->info = src->info;
192 Dump (access, buffer, len);
198 static block_t *Block (access_t *access)
200 access_t *src = access->p_source;
203 src->info.i_update = access->info.i_update;
204 block = src->pf_block (src);
205 access->info = src->info;
207 if ((block == NULL) || (block->i_buffer <= 0))
210 Dump (access, block->p_buffer, block->i_buffer);
216 static int Control (access_t *access, int cmd, va_list ap)
218 access_t *src = access->p_source;
220 return src->pf_control (src, cmd, ap);
224 static int Seek (access_t *access, int64_t offset)
226 access_t *src = access->p_source;
227 access_sys_t *p_sys = access->p_sys;
229 if (p_sys->tmp_max == -1)
231 msg_Err (access, "cannot seek while dumping!");
235 if (p_sys->stream != NULL)
236 msg_Dbg (access, "seeking - dump might not work");
238 src->info.i_update = access->info.i_update;
239 int ret = src->pf_seek (src, offset);
240 access->info = src->info;
245 #ifndef HAVE_LOCALTIME_R
246 static inline struct tm *localtime_r (const time_t *now, struct tm *res)
248 struct tm *unsafe = localtime (now);
250 * This is not thread-safe. Blame your C library.
251 * On Win32 there SHOULD be _localtime_s instead, but of course
252 * Cygwin and Mingw32 don't know about it. You're on your own if you
258 memcpy (res, unsafe, sizeof (*res));
264 static void Trigger (access_t *access)
266 access_sys_t *p_sys = access->p_sys;
268 if (p_sys->stream == NULL)
271 if (p_sys->tmp_max == -1)
272 return; // already triggered - should we stop? FIXME
278 if (localtime_r (&now, &t) == NULL)
279 return; // No time, eh? Well, I'd rather not run on your computer.
281 if (t.tm_year > 999999999)
282 // Humanity is about 300 times older than when this was written,
283 // and there is an off-by-one in the following sprintf().
286 const char *home = access->p_libvlc->psz_homedir;
288 /* Hmm what about the extension?? */
289 char filename[strlen (home) + sizeof ("/vlcdump-YYYYYYYYY-MM-DD-HH-MM-SS.ts")];
290 sprintf (filename, "%s/vlcdump-%04u-%02u-%02u-%02u-%02u-%02u.ts", home,
291 t.tm_year, t.tm_mon, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec);
293 msg_Info (access, "dumping media to \"%s\"...", filename);
295 FILE *newstream = utf8_fopen (filename, "wb");
296 if (newstream == NULL)
298 msg_Err (access, "cannot create dump file \"%s\": %s", filename,
303 /* This might cause excessive hard drive work :( */
304 FILE *oldstream = p_sys->stream;
310 size_t len = fread (buf, 1, sizeof (buf), oldstream);
313 if (ferror (oldstream))
315 msg_Err (access, "cannot read temporary file: %s",
320 /* Done with temporary file */
322 p_sys->stream = newstream;
327 if (fwrite (buf, len, 1, newstream) != 1)
329 msg_Err (access, "cannot write dump file: %s", strerror (errno));
334 /* Failed to copy temporary file */
335 fseek (oldstream, 0, SEEK_END);
341 static int KeyHandler (vlc_object_t *obj, char const *varname,
342 vlc_value_t oldval, vlc_value_t newval, void *data)
344 access_t *access = data;
349 for (struct hotkey *key = access->p_libvlc->p_hotkeys;
350 key->psz_action != NULL; key++)
352 if (key->i_key == newval.i_int)
354 if (key->i_action == ACTIONID_DUMP)
355 Trigger ((access_t *)data);