]> git.sesse.net Git - vlc/blob - modules/access_filter/dump.c
Add dump access filter to save original media to home
[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
31 #include "vlc_access.h"
32 #include "vlc_block.h"
33 #include "charset.h"
34 #include "vlc_keys.h"
35
36 #define DEFAULT_MARGIN 32 // megabytes
37
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.")
41
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.")
45
46 static int  Open (vlc_object_t *);
47 static void Close (vlc_object_t *);
48
49 vlc_module_begin ();
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);
57
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);
62 vlc_module_end();
63
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);
68
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);
72
73 struct access_sys_t
74 {
75     FILE *stream;
76     int64_t tmp_max;
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) || (len == 0))
148         return;
149
150     if ((p_sys->tmp_max != -1) && (access->info.i_pos > p_sys->tmp_max))
151     {
152         msg_Dbg (access, "too much data - dump will not work");
153         goto error;
154     }
155
156     if (fwrite (buffer, len, 1, stream) != 1)
157     {
158         msg_Err (access, "cannot write to file: %s", strerror (errno));
159         goto error;
160     }
161
162     return;
163
164 error:
165     fclose (stream);
166     p_sys->stream = NULL;
167 }
168
169
170 static int Read (access_t *access, uint8_t *buffer, int len)
171 {
172     access_t *src = access->p_source;
173
174     src->info.i_update = access->info.i_update;
175     len = src->pf_read (src, buffer, len);
176     access->info = src->info;
177
178     Dump (access, buffer, len);
179     //Trigger (access);
180
181     return len;
182 }
183
184
185 static block_t *Block (access_t *access)
186 {
187     access_t *src = access->p_source;
188     block_t *block;
189
190     src->info.i_update = access->info.i_update;
191     block = src->pf_block (src);
192     access->info = src->info;
193
194     if ((block == NULL) || (block->i_buffer <= 0))
195         return block;
196
197     Dump (access, block->p_buffer, block->i_buffer);
198
199     return block;
200 }
201
202
203 static int Control (access_t *access, int cmd, va_list ap)
204 {
205     access_t *src = access->p_source;
206
207     return src->pf_control (src, cmd, ap);
208 }
209
210
211 static int Seek (access_t *access, int64_t offset)
212 {
213     access_t *src = access->p_source;
214     access_sys_t *p_sys = access->p_sys;
215
216     if (p_sys->tmp_max == -1)
217     {
218         msg_Err (access, "cannot seek while dumping!");
219         return VLC_EGENERIC;
220     }
221
222     if (p_sys->stream != NULL)
223     {
224         msg_Dbg (access, "seeking - dump will not work");
225         fclose (p_sys->stream);
226         p_sys->stream = NULL;
227     }
228
229     src->info.i_update = access->info.i_update;
230     int ret = src->pf_seek (src, offset);
231     access->info = src->info;
232     return ret;
233 }
234
235
236 static void Trigger (access_t *access)
237 {
238     access_sys_t *p_sys = access->p_sys;
239
240     if (p_sys->stream == NULL)
241         return; // too late
242
243     if (p_sys->tmp_max == -1)
244         return; // already triggered - should we stop? FIXME
245
246     time_t now;
247     time (&now);
248
249     struct tm t;
250     if (localtime_r (&now, &t) == NULL)
251         return; // No time, eh? Well, I'd rather not run on your computer.
252
253     if (t.tm_year > 999999999)
254         // Humanity is about 300 times older than when this was written,
255         // and there is an off-by-one in the following sprintf().
256         return;
257
258     const char *home = access->p_libvlc->psz_homedir;
259
260     /* Hmm what about the extension?? */
261     char filename[strlen (home) + sizeof ("/vlcdump-YYYYYYYYY-MM-DD-HH-MM-SS.ts")];
262     sprintf (filename, "%s/vlcdump-%04u-%02u-%02u-%02u-%02u-%02u.ts", home,
263              t.tm_year, t.tm_mon, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec);
264
265     msg_Info (access, "dumping media to \"%s\"...", filename);
266
267     FILE *newstream = fopen (filename, "wb");
268     if (newstream == NULL)
269     {
270         msg_Err (access, "cannot create dump file \"%s\": %s", filename,
271                  strerror (errno));
272         return;
273     }
274
275     /* This might cause excessive hard drive work :( */
276     FILE *oldstream = p_sys->stream;
277     rewind (oldstream);
278
279     for (;;)
280     {
281         char buf[16384];
282         size_t len = fread (buf, 1, sizeof (buf), oldstream);
283         if (len == 0)
284         {
285             if (ferror (oldstream))
286             {
287                 msg_Err (access, "cannot read temporary file: %s",
288                          strerror (errno));
289                 break;
290             }
291
292             /* Done with temporary file */
293             fclose (oldstream);
294             p_sys->stream = newstream;
295             p_sys->tmp_max = -1;
296             return;
297         }
298
299         if (fwrite (buf, len, 1, newstream) != 1)
300         {
301             msg_Err (access, "cannot write dump file: %s", strerror (errno));
302             break;
303         }
304     }
305
306     /* Failed to copy temporary file */
307     fseek (oldstream, 0, SEEK_END);
308     fclose (newstream);
309     return;
310 }
311
312
313 static int KeyHandler (vlc_object_t *obj, char const *varname,
314                        vlc_value_t oldval, vlc_value_t newval, void *data)
315 {
316     access_t *access = data;
317
318     (void)oldval;
319     (void)obj;
320
321     for (struct hotkey *key = access->p_libvlc->p_hotkeys;
322          key->psz_action != NULL; key++)
323     {
324         if (key->i_key == newval.i_int)
325         {
326             if (key->i_action == ACTIONID_DUMP)
327                 Trigger ((access_t *)data);
328             break;
329         }
330     }
331
332     return VLC_SUCCESS;
333 }