| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
#ifdef HAVE_CONFIG_H |
|---|
| 25 |
# include "config.h" |
|---|
| 26 |
#endif |
|---|
| 27 |
|
|---|
| 28 |
#include <vlc_common.h> |
|---|
| 29 |
#include <vlc_plugin.h> |
|---|
| 30 |
|
|---|
| 31 |
#include <assert.h> |
|---|
| 32 |
#include <time.h> |
|---|
| 33 |
#include <errno.h> |
|---|
| 34 |
|
|---|
| 35 |
#include <vlc_access.h> |
|---|
| 36 |
|
|---|
| 37 |
#include <vlc_charset.h> |
|---|
| 38 |
#include "vlc_keys.h" |
|---|
| 39 |
|
|---|
| 40 |
#define DEFAULT_MARGIN 32 // megabytes |
|---|
| 41 |
|
|---|
| 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.") |
|---|
| 45 |
|
|---|
| 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.") |
|---|
| 49 |
|
|---|
| 50 |
static int Open (vlc_object_t *); |
|---|
| 51 |
static void Close (vlc_object_t *); |
|---|
| 52 |
|
|---|
| 53 |
vlc_module_begin (); |
|---|
| 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); |
|---|
| 59 |
add_shortcut ("dump"); |
|---|
| 60 |
set_callbacks (Open, Close); |
|---|
| 61 |
|
|---|
| 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); |
|---|
| 66 |
vlc_module_end(); |
|---|
| 67 |
|
|---|
| 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); |
|---|
| 72 |
|
|---|
| 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); |
|---|
| 76 |
|
|---|
| 77 |
struct access_sys_t |
|---|
| 78 |
{ |
|---|
| 79 |
FILE *stream; |
|---|
| 80 |
int64_t tmp_max; |
|---|
| 81 |
int64_t dumpsize; |
|---|
| 82 |
}; |
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| 87 |
static int Open (vlc_object_t *obj) |
|---|
| 88 |
{ |
|---|
| 89 |
access_t *access = (access_t*)obj; |
|---|
| 90 |
access_t *src = access->p_source; |
|---|
| 91 |
|
|---|
| 92 |
if (!var_CreateGetBool (access, "dump-force")) |
|---|
| 93 |
{ |
|---|
| 94 |
bool b; |
|---|
| 95 |
if ((access_Control (src, ACCESS_CAN_FASTSEEK, &b) == 0) && b) |
|---|
| 96 |
{ |
|---|
| 97 |
msg_Dbg (obj, "dump filter useless"); |
|---|
| 98 |
return VLC_EGENERIC; |
|---|
| 99 |
} |
|---|
| 100 |
} |
|---|
| 101 |
|
|---|
| 102 |
if (src->pf_read != NULL) |
|---|
| 103 |
access->pf_read = Read; |
|---|
| 104 |
else |
|---|
| 105 |
access->pf_block = Block; |
|---|
| 106 |
if (src->pf_seek != NULL) |
|---|
| 107 |
access->pf_seek = Seek; |
|---|
| 108 |
|
|---|
| 109 |
access->pf_control = Control; |
|---|
| 110 |
access->info = src->info; |
|---|
| 111 |
|
|---|
| 112 |
access_sys_t *p_sys = access->p_sys = malloc (sizeof (*p_sys)); |
|---|
| 113 |
if (p_sys == NULL) |
|---|
| 114 |
return VLC_ENOMEM; |
|---|
| 115 |
memset (p_sys, 0, sizeof (*p_sys)); |
|---|
| 116 |
|
|---|
| 117 |
if ((p_sys->stream = tmpfile ()) == NULL) |
|---|
| 118 |
{ |
|---|
| 119 |
msg_Err (access, "cannot create temporary file: %m"); |
|---|
| 120 |
free (p_sys); |
|---|
| 121 |
return VLC_EGENERIC; |
|---|
| 122 |
} |
|---|
| 123 |
p_sys->tmp_max = ((int64_t)var_CreateGetInteger (access, "dump-margin")) << 20; |
|---|
| 124 |
|
|---|
| 125 |
var_AddCallback (access->p_libvlc, "key-action", KeyHandler, access); |
|---|
| 126 |
|
|---|
| 127 |
return VLC_SUCCESS; |
|---|
| 128 |
} |
|---|
| 129 |
|
|---|
| 130 |
|
|---|
| 131 |
|
|---|
| 132 |
|
|---|
| 133 |
|
|---|
| 134 |
static void Close (vlc_object_t *obj) |
|---|
| 135 |
{ |
|---|
| 136 |
access_t *access = (access_t *)obj; |
|---|
| 137 |
access_sys_t *p_sys = access->p_sys; |
|---|
| 138 |
|
|---|
| 139 |
var_DelCallback (access->p_libvlc, "key-action", KeyHandler, access); |
|---|
| 140 |
|
|---|
| 141 |
if (p_sys->stream != NULL) |
|---|
| 142 |
fclose (p_sys->stream); |
|---|
| 143 |
free (p_sys); |
|---|
| 144 |
} |
|---|
| 145 |
|
|---|
| 146 |
|
|---|
| 147 |
static void Dump (access_t *access, const uint8_t *buffer, size_t len) |
|---|
| 148 |
{ |
|---|
| 149 |
access_sys_t *p_sys = access->p_sys; |
|---|
| 150 |
FILE *stream = p_sys->stream; |
|---|
| 151 |
|
|---|
| 152 |
if ((stream == NULL) |
|---|
| 153 |
|| (access->info.i_pos < p_sys->dumpsize) ) |
|---|
| 154 |
return; |
|---|
| 155 |
|
|---|
| 156 |
size_t needed = access->info.i_pos - p_sys->dumpsize; |
|---|
| 157 |
if (len < needed) |
|---|
| 158 |
return; |
|---|
| 159 |
|
|---|
| 160 |
buffer += len - needed; |
|---|
| 161 |
len = needed; |
|---|
| 162 |
|
|---|
| 163 |
if (len == 0) |
|---|
| 164 |
return; |
|---|
| 165 |
|
|---|
| 166 |
if ((p_sys->tmp_max != -1) && (access->info.i_pos > p_sys->tmp_max)) |
|---|
| 167 |
{ |
|---|
| 168 |
msg_Dbg (access, "too much data - dump will not work"); |
|---|
| 169 |
goto error; |
|---|
| 170 |
} |
|---|
| 171 |
|
|---|
| 172 |
assert (len > 0); |
|---|
| 173 |
if (fwrite (buffer, len, 1, stream) != 1) |
|---|
| 174 |
{ |
|---|
| 175 |
msg_Err (access, "cannot write to file: %m"); |
|---|
| 176 |
goto error; |
|---|
| 177 |
} |
|---|
| 178 |
|
|---|
| 179 |
p_sys->dumpsize += len; |
|---|
| 180 |
return; |
|---|
| 181 |
|
|---|
| 182 |
error: |
|---|
| 183 |
fclose (stream); |
|---|
| 184 |
p_sys->stream = NULL; |
|---|
| 185 |
} |
|---|
| 186 |
|
|---|
| 187 |
|
|---|
| 188 |
static ssize_t Read (access_t *access, uint8_t *buffer, size_t len) |
|---|
| 189 |
{ |
|---|
| 190 |
access_t *src = access->p_source; |
|---|
| 191 |
|
|---|
| 192 |
src->info.i_update = access->info.i_update; |
|---|
| 193 |
len = src->pf_read (src, buffer, len); |
|---|
| 194 |
access->info = src->info; |
|---|
| 195 |
|
|---|
| 196 |
Dump (access, buffer, len); |
|---|
| 197 |
|
|---|
| 198 |
return len; |
|---|
| 199 |
} |
|---|
| 200 |
|
|---|
| 201 |
|
|---|
| 202 |
static block_t *Block (access_t *access) |
|---|
| 203 |
{ |
|---|
| 204 |
access_t *src = access->p_source; |
|---|
| 205 |
block_t *block; |
|---|
| 206 |
|
|---|
| 207 |
src->info.i_update = access->info.i_update; |
|---|
| 208 |
block = src->pf_block (src); |
|---|
| 209 |
access->info = src->info; |
|---|
| 210 |
|
|---|
| 211 |
if ((block == NULL) || (block->i_buffer <= 0)) |
|---|
| 212 |
return block; |
|---|
| 213 |
|
|---|
| 214 |
Dump (access, block->p_buffer, block->i_buffer); |
|---|
| 215 |
|
|---|
| 216 |
return block; |
|---|
| 217 |
} |
|---|
| 218 |
|
|---|
| 219 |
|
|---|
| 220 |
static int Control (access_t *access, int cmd, va_list ap) |
|---|
| 221 |
{ |
|---|
| 222 |
access_t *src = access->p_source; |
|---|
| 223 |
|
|---|
| 224 |
return src->pf_control (src, cmd, ap); |
|---|
| 225 |
} |
|---|
| 226 |
|
|---|
| 227 |
|
|---|
| 228 |
static int Seek (access_t *access, int64_t offset) |
|---|
| 229 |
{ |
|---|
| 230 |
access_t *src = access->p_source; |
|---|
| 231 |
access_sys_t *p_sys = access->p_sys; |
|---|
| 232 |
|
|---|
| 233 |
if (p_sys->tmp_max == -1) |
|---|
| 234 |
{ |
|---|
| 235 |
msg_Err (access, "cannot seek while dumping!"); |
|---|
| 236 |
return VLC_EGENERIC; |
|---|
| 237 |
} |
|---|
| 238 |
|
|---|
| 239 |
if (p_sys->stream != NULL) |
|---|
| 240 |
msg_Dbg (access, "seeking - dump might not work"); |
|---|
| 241 |
|
|---|
| 242 |
src->info.i_update = access->info.i_update; |
|---|
| 243 |
int ret = src->pf_seek (src, offset); |
|---|
| 244 |
access->info = src->info; |
|---|
| 245 |
return ret; |
|---|
| 246 |
} |
|---|
| 247 |
|
|---|
| 248 |
static void Trigger (access_t *access) |
|---|
| 249 |
{ |
|---|
| 250 |
access_sys_t *p_sys = access->p_sys; |
|---|
| 251 |
|
|---|
| 252 |
if (p_sys->stream == NULL) |
|---|
| 253 |
return; |
|---|
| 254 |
|
|---|
| 255 |
if (p_sys->tmp_max == -1) |
|---|
| 256 |
return; |
|---|
| 257 |
|
|---|
| 258 |
time_t now; |
|---|
| 259 |
time (&now); |
|---|
| 260 |
|
|---|
| 261 |
struct tm t; |
|---|
| 262 |
if (localtime_r (&now, &t) == NULL) |
|---|
| 263 |
return; |
|---|
| 264 |
|
|---|
| 265 |
if (t.tm_year > 999999999) |
|---|
| 266 |
|
|---|
| 267 |
|
|---|
| 268 |
return; |
|---|
| 269 |
|
|---|
| 270 |
const char *home = config_GetHomeDir(); |
|---|
| 271 |
|
|---|
| 272 |
|
|---|
| 273 |
char filename[strlen (home) + sizeof ("/vlcdump-YYYYYYYYY-MM-DD-HH-MM-SS.ts")]; |
|---|
| 274 |
sprintf (filename, "%s/vlcdump-%04u-%02u-%02u-%02u-%02u-%02u.ts", home, |
|---|
| 275 |
t.tm_year, t.tm_mon, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec); |
|---|
| 276 |
|
|---|
| 277 |
msg_Info (access, "dumping media to \"%s\"...", filename); |
|---|
| 278 |
|
|---|
| 279 |
FILE *newstream = utf8_fopen (filename, "wb"); |
|---|
| 280 |
if (newstream == NULL) |
|---|
| 281 |
{ |
|---|
| 282 |
msg_Err (access, "cannot create dump file \"%s\": %m", filename); |
|---|
| 283 |
return; |
|---|
| 284 |
} |
|---|
| 285 |
|
|---|
| 286 |
|
|---|
| 287 |
FILE *oldstream = p_sys->stream; |
|---|
| 288 |
rewind (oldstream); |
|---|
| 289 |
|
|---|
| 290 |
for (;;) |
|---|
| 291 |
{ |
|---|
| 292 |
char buf[16384]; |
|---|
| 293 |
size_t len = fread (buf, 1, sizeof (buf), oldstream); |
|---|
| 294 |
if (len == 0) |
|---|
| 295 |
{ |
|---|
| 296 |
if (ferror (oldstream)) |
|---|
| 297 |
{ |
|---|
| 298 |
msg_Err (access, "cannot read temporary file: %m"); |
|---|
| 299 |
break; |
|---|
| 300 |
} |
|---|
| 301 |
|
|---|
| 302 |
|
|---|
| 303 |
fclose (oldstream); |
|---|
| 304 |
p_sys->stream = newstream; |
|---|
| 305 |
p_sys->tmp_max = -1; |
|---|
| 306 |
return; |
|---|
| 307 |
} |
|---|
| 308 |
|
|---|
| 309 |
if (fwrite (buf, len, 1, newstream) != 1) |
|---|
| 310 |
{ |
|---|
| 311 |
msg_Err (access, "cannot write dump file: %m"); |
|---|
| 312 |
break; |
|---|
| 313 |
} |
|---|
| 314 |
} |
|---|
| 315 |
|
|---|
| 316 |
|
|---|
| 317 |
fseek (oldstream, 0, SEEK_END); |
|---|
| 318 |
fclose (newstream); |
|---|
| 319 |
return; |
|---|
| 320 |
} |
|---|
| 321 |
|
|---|
| 322 |
|
|---|
| 323 |
static int KeyHandler (vlc_object_t *obj, char const *varname, |
|---|
| 324 |
vlc_value_t oldval, vlc_value_t newval, void *data) |
|---|
| 325 |
{ |
|---|
| 326 |
access_t *access = data; |
|---|
| 327 |
|
|---|
| 328 |
(void)varname; |
|---|
| 329 |
(void)oldval; |
|---|
| 330 |
(void)obj; |
|---|
| 331 |
|
|---|
| 332 |
if (newval.i_int == ACTIONID_DUMP) |
|---|
| 333 |
Trigger (access); |
|---|
| 334 |
return VLC_SUCCESS; |
|---|
| 335 |
} |
|---|