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 *****************************************************************************/
28 #include <vlc_common.h>
29 #include <vlc_plugin.h>
35 #include <vlc_access.h>
37 #include <vlc_charset.h>
40 #define DEFAULT_MARGIN 32 // megabytes
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.")
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.")
50 static int Open (vlc_object_t *);
51 static void Close (vlc_object_t *);
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)
60 set_callbacks (Open, Close)
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);
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);
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);
87 static int Open (vlc_object_t *obj)
89 access_t *access = (access_t*)obj;
90 access_t *src = access->p_source;
92 if (!var_CreateGetBool (access, "dump-force"))
95 if ((access_Control (src, ACCESS_CAN_FASTSEEK, &b) == 0) && b)
97 msg_Dbg (obj, "dump filter useless");
102 if (src->pf_read != NULL)
103 access->pf_read = Read;
105 access->pf_block = Block;
106 if (src->pf_seek != NULL)
107 access->pf_seek = Seek;
109 access->pf_control = Control;
110 access->info = src->info;
112 access_sys_t *p_sys = access->p_sys = malloc (sizeof (*p_sys));
115 memset (p_sys, 0, sizeof (*p_sys));
118 if ((p_sys->stream = tmpfile ()) == NULL)
121 if(GetTempFileName("\\Temp\\","vlc",0,buf) || ((p_sys->stream = fopen(buf,"wb+")) ==NULL))
124 msg_Err (access, "cannot create temporary file: %m");
128 p_sys->tmp_max = ((int64_t)var_CreateGetInteger (access, "dump-margin")) << 20;
130 var_AddCallback (access->p_libvlc, "key-action", KeyHandler, access);
139 static void Close (vlc_object_t *obj)
141 access_t *access = (access_t *)obj;
142 access_sys_t *p_sys = access->p_sys;
144 var_DelCallback (access->p_libvlc, "key-action", KeyHandler, access);
146 if (p_sys->stream != NULL)
147 fclose (p_sys->stream);
152 static void Dump (access_t *access, const uint8_t *buffer, size_t len)
154 access_sys_t *p_sys = access->p_sys;
155 FILE *stream = p_sys->stream;
157 if ((stream == NULL) /* not dumping */
158 || (access->info.i_pos < p_sys->dumpsize) /* already known data */)
161 size_t needed = access->info.i_pos - p_sys->dumpsize;
163 return; /* gap between data and dump offset (seek too far ahead?) */
165 buffer += len - needed;
169 return; /* no useful data */
171 if ((p_sys->tmp_max != -1) && (access->info.i_pos > p_sys->tmp_max))
173 msg_Dbg (access, "too much data - dump will not work");
178 if (fwrite (buffer, len, 1, stream) != 1)
180 msg_Err (access, "cannot write to file: %m");
184 p_sys->dumpsize += len;
189 p_sys->stream = NULL;
193 static ssize_t Read (access_t *access, uint8_t *buffer, size_t len)
195 access_t *src = access->p_source;
197 src->info.i_update = access->info.i_update;
198 len = src->pf_read (src, buffer, len);
199 access->info = src->info;
201 Dump (access, buffer, len);
207 static block_t *Block (access_t *access)
209 access_t *src = access->p_source;
212 src->info.i_update = access->info.i_update;
213 block = src->pf_block (src);
214 access->info = src->info;
216 if ((block == NULL) || (block->i_buffer <= 0))
219 Dump (access, block->p_buffer, block->i_buffer);
225 static int Control (access_t *access, int cmd, va_list ap)
227 access_t *src = access->p_source;
229 return src->pf_control (src, cmd, ap);
233 static int Seek (access_t *access, int64_t offset)
235 access_t *src = access->p_source;
236 access_sys_t *p_sys = access->p_sys;
238 if (p_sys->tmp_max == -1)
240 msg_Err (access, "cannot seek while dumping!");
244 if (p_sys->stream != NULL)
245 msg_Dbg (access, "seeking - dump might not work");
247 src->info.i_update = access->info.i_update;
248 int ret = src->pf_seek (src, offset);
249 access->info = src->info;
253 static void Trigger (access_t *access)
255 access_sys_t *p_sys = access->p_sys;
257 if (p_sys->stream == NULL)
260 if (p_sys->tmp_max == -1)
261 return; // already triggered - should we stop? FIXME
267 if (localtime_r (&now, &t) == NULL)
268 return; // No time, eh? Well, I'd rather not run on your computer.
270 if (t.tm_year > 999999999)
271 // Humanity is about 300 times older than when this was written,
272 // and there is an off-by-one in the following sprintf().
275 const char *home = config_GetHomeDir();
277 /* Hmm what about the extension?? */
278 char filename[strlen (home) + sizeof ("/vlcdump-YYYYYYYYY-MM-DD-HH-MM-SS.ts")];
279 sprintf (filename, "%s/vlcdump-%04u-%02u-%02u-%02u-%02u-%02u.ts", home,
280 t.tm_year, t.tm_mon, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec);
282 msg_Info (access, "dumping media to \"%s\"...", filename);
284 FILE *newstream = utf8_fopen (filename, "wb");
285 if (newstream == NULL)
287 msg_Err (access, "cannot create dump file \"%s\": %m", filename);
291 /* This might cause excessive hard drive work :( */
292 FILE *oldstream = p_sys->stream;
298 size_t len = fread (buf, 1, sizeof (buf), oldstream);
301 if (ferror (oldstream))
303 msg_Err (access, "cannot read temporary file: %m");
307 /* Done with temporary file */
309 p_sys->stream = newstream;
314 if (fwrite (buf, len, 1, newstream) != 1)
316 msg_Err (access, "cannot write dump file: %m");
321 /* Failed to copy temporary file */
322 fseek (oldstream, 0, SEEK_END);
328 static int KeyHandler (vlc_object_t *obj, char const *varname,
329 vlc_value_t oldval, vlc_value_t newval, void *data)
331 access_t *access = data;
337 if (newval.i_int == ACTIONID_DUMP)